cli icon indicating copy to clipboard operation
cli copied to clipboard

Add bindMaps feature (#3786)

Open ikedam opened this issue 3 years ago • 0 comments

- What I did

This change allows host paths in bind mounts specified in docker command line automatically mapped to the paths suitable in hosts where dockerd runs.

- How I did it

I introduced a new "bindMaps" member in .docker/config.json. It defines paths to map.

For example, if your local path /home/ikedam is actually located on /exports/ikedam on the host where dockerd run, your .docker/config.json would be like this:

{
  "bindMaps": {
    "/home/ikedam": "/exports/ikedam"
  }
}

That configuration converts docker run -v "/home/ikedam/work:/worksapce" ... to docker run -v "/exports/ikedam/work:/workspace", and that would work correct on the remote machine. It would be especially useful when used with docker run -v "./work:/workspace" ...ordocker run -v "$(pwd)/work:/workspace" ...`.

- How to verify it

Instructions to test on Windows and WSL2:

  • Build docker command:

    • checkout this branch.

    • Build a command for Windows (This must be performed on an environment with docker):

      docker buildx bake --set binary.platform=windows/amd64
      
    • build\docker-windows-amd64.exe will be available and rename that to docker.exe.

  • Set up WSL2 distribution (You can skip here if you have already an appropriate WSL2 distribution available):

    • Set the version of WSL to 2.

      wsl --set-default-version 2
      
    • Install Ubuntu-20.04.

       wsl --install -d Ubuntu-20.04
      
    • Set up your unix user.

  • Install dockerd on WSL2:

    • Install Docker Engine following the instructions in https://docs.docker.com/engine/install/ubuntu/

      • Note: dockerd doesn't launch automatically.
    • Configure dockerd to accept 127.0.0.1:2375 (for access from Windows)

      $ cat /etc/default/docker
      ...
      DOCKER_OPTS="-H unix:///var/run/docker.sock -H tcp://127.0.0.1:2375"
      ...
      
    • Start dockerd. NOTE: dockerd doesn't start automatically, and you must start it every time you log onto Windows.

      $ sudo service docker start
      
  • Test bindMaps feature from Windows:

    • Create %USERPROFILE%.docker\config.json like this:

      {
        "bindMaps": {
          "default": {
            "C:\\": "/mnt/c"
          }
        }
      }
      
    • Configure DOCKER_HOST to connect to dockerd on WSL2:

      > ${ENV:DOCKER_HOST}="tcp://127.0.0.1:2375"
      
      • expected to be run on Powershell.
    • Run docker command. Consider built docker command is available in the current directory:

      > echo "test!" >index.html
      > .\docker run --rm -p 8080:80 -v ".:/usr/local/apache2/htdocs" --name httpd -d httpd:2.4-alpine
      > Invoke-WebRequest http://127.0.0.1:8080
      > .\docker stop httpd
      
      • The output from Invoke-WebResuest would contain "test!". That means bindMaps feature allows you to specify bind mount with Windows paths, even though the docker daemon run on a WSL2 distribution.

- Description for the changelog

Add bindMaps feature: host paths in bind mounts can be mapped to another paths, like actual paths on the remote machine

closes #3786

ikedam avatar Sep 24 '22 07:09 ikedam