Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bindMaps feature (#3786) #3787

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

ikedam
Copy link

@ikedam ikedam commented Sep 24, 2022

- 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
Copy link
Author

ikedam commented Dec 4, 2022

I found this didn't work with --mount option like:

docker run --rm -p 8080:80 --mount "type=bind,source=$(pwd),target=/usr/local/apache2/htdocs,consistency=cached" --name httpd -d httpd:2.4-alpine

(VSCode Dev Containers extension uses this option)

I update the implementation and now this supports --mount options.
And also resolved the conflict by rebasing to master.
The old implementation is available in: https://github.com/ikedam/cli/commits/3786-bindMaps.old

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Auto-mappings for host paths in bind mount option
2 participants