How do I mount a Docker volume while using a Windows host?

23,106

Solution 1

It is possible the / is interpreted as an option by the CMD Windows shell.

Try first a docker-machine ssh default, in order to open an ssh session in your VM. From there try the docker run again: docker run -v /c/Users/phisch/dev/htdocs:/var/www phisch:dev

As commented by thaJeztah in issue 18290:

You could consider using docker-compose; docker-compose allows you to define bind-mounted volumes relative to the location of the docker-compose.yml file.
Using a docker-compose file allows you to specify all options needed to run your containers in a single file, which makes it ideal for sharing between team members (ie, just run docker-compose up -d will start all containers for the project with the right options).

This comment mentions a&dding a second /:

docker run -v //c/Users/phisch/dev/htdocs:`/var/www` phisch:dev

Even in the docker toolbox msys shell session, there are issues (like issue 282)

Pyetro notes in the comments:

In Windows, a double slash is needed at the beginning of the path to indicate the working directory.
Just to work with short path use like this:

docker run -v //$(PWD)/folder:/folder ...

After lengthy discussion, the issue was that /var/www had a folder in it.

Mounting /c/Users/phisch/dev/htdoc onto an empty folder does work, but might not give the expected result, as the default CMD apache2-foreground might still serve its content based on /var/www (which would not have htdocs content if that htdocs is mounted onto another folder).

Solution 2

I use Docker for Windows with PowerShell and use $PWD to refer to the current directory when mounting a volume, and it works well for me. A couple of examples:

docker run -p 2368:2368 -v $PWD/ghost:/var/lib/ghost -d ghost

docker run -p 2368:2368 -v $PWD/:/var/lib/ghost -d ghost

Solution 3

If we are talking about Docker on Windows then we have to take in account the fact that all containers are run on VirtualBox.

Before mounting volume to a container we have to be sure that particular folder is available for VirtualBox.

Firstly, to define the name of the current running Docker machine, run

$ docker-machine.exe  active
default

Secondly, add shared folder to VirtualBox:

$ VBoxManage sharedfolder add default --name "some_project" --hostpath D:\Projects\some_project

Thirdly, create the folder

$ docker-machine.exe ssh default 'sudo mkdir --parents /d/projects/some_project'

Fourthly, mount it:

$ docker-machine.exe ssh default 'sudo mount -t vboxsf some_project /d/projects/some_project'

Finally, create a container:

$ docker run -v //d/projects/some_project://d/projects/some_project -d some-image_name
Share:
23,106
Philipp Schaffrath
Author by

Philipp Schaffrath

Updated on July 12, 2022

Comments

  • Philipp Schaffrath
    Philipp Schaffrath almost 2 years

    Mounting a Docker volume while being under a Windows host, has been a huge pain for me, and I could not get it to work.

    Currently I got the following simple Dockerfile:

    FROM php:5-apache
    RUN apt-get update
    

    When I build an image from it, and start a container

    docker build -t phisch:dev .
    docker run phisch:dev
    

    the container starts properly.

    But when I am trying to mount a volume,

    docker run -v /c/Users/phisch/dev/htdocs:/var/www phisch:dev
    

    the following message will be displayed:

    C:\Users\phisch\dev>docker run -v /c/Users/phisch/dev/htdocs:/var/www phisch:dev
    no such file or directory
    docker: Error response from daemon: Container command not found or does not exist..
    

    The /var/www directory definitely exists in the container, and trying other directories does not change the result. Prepending a trailing slash to the host-side directory does not help either. (//c/Users/phisch/dev/htdocs)

    How do I mount for example /var/www to C:/Users/phisch/dev/htdocs?

    phisch@DESKTOP-UC1LB9J MINGW64 ~/dev (master)
    $ docker inspect phisch:dev
    [
        {
            "Id": "sha256:73c1533222a905a378f12505ccbd9e9b34cde5a4b34ed008c39e23d5d58a9c91",
            "RepoTags": [
                "dev_web:latest",
                "phisch:dev",
                "phisch:dev3"
            ],
            "RepoDigests": [],
            "Parent": "sha256:d2c4149d86c4dfceaff0e9c4eb5a5d42ca7815f81dd08baad4dc8bda6db2fb10",
            "Comment": "",
            "Created": "2016-02-10T12:16:37.667236134Z",
            "Container": "dad811f51ef3b94d9845d13a0e43ad07ccd5684ea2747b3846accdc71abeb628",
            "ContainerConfig": {
                "Hostname": "e06f5a03fe1f",
                "Domainname": "",
                "User": "",
                "AttachStdin": false,
                "AttachStdout": false,
                "AttachStderr": false,
                "ExposedPorts": {
                    "80/tcp": {}
                },
                "Tty": false,
                "OpenStdin": false,
                "StdinOnce": false,
                "Env": [
                    "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                    "PHP_INI_DIR=/usr/local/etc/php",
                    "PHP_EXTRA_BUILD_DEPS=apache2-dev",
                    "PHP_EXTRA_CONFIGURE_ARGS=--with-apxs2",
                    "GPG_KEYS=0BD78B5F97500D450838F95DFE857D9A90D90EC1 6E4F6AB321FDC07F2C332E3AC2BF0BC433CFC8B3",
                    "PHP_VERSION=5.6.18",
                    "PHP_FILENAME=php-5.6.18.tar.xz",
                    "PHP_SHA256=54dd9106c3469bc7028644d72ac140af00655420bbaaf4a742a64e9ed02ec1b0"
                ],
                "Cmd": [
                    "/bin/sh",
                    "-c",
                    "apt-get update"
                ],
                "ArgsEscaped": true,
                "Image": "sha256:d2c4149d86c4dfceaff0e9c4eb5a5d42ca7815f81dd08baad4dc8bda6db2fb10",
                "Volumes": null,
                "WorkingDir": "/var/www/html",
                "Entrypoint": null,
                "OnBuild": [],
                "Labels": {}
            },
            "DockerVersion": "1.10.0",
            "Author": "",
            "Config": {
                "Hostname": "e06f5a03fe1f",
                "Domainname": "",
                "User": "",
                "AttachStdin": false,
                "AttachStdout": false,
                "AttachStderr": false,
                "ExposedPorts": {
                    "80/tcp": {}
                },
                "Tty": false,
                "OpenStdin": false,
                "StdinOnce": false,
                "Env": [
                    "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                    "PHP_INI_DIR=/usr/local/etc/php",
                    "PHP_EXTRA_BUILD_DEPS=apache2-dev",
                    "PHP_EXTRA_CONFIGURE_ARGS=--with-apxs2",
                    "GPG_KEYS=0BD78B5F97500D450838F95DFE857D9A90D90EC1 6E4F6AB321FDC07F2C332E3AC2BF0BC433CFC8B3",
                    "PHP_VERSION=5.6.18",
                    "PHP_FILENAME=php-5.6.18.tar.xz",
                    "PHP_SHA256=54dd9106c3469bc7028644d72ac140af00655420bbaaf4a742a64e9ed02ec1b0"
                ],
                "Cmd": [
                    "apache2-foreground"
                ],
                "ArgsEscaped": true,
                "Image": "sha256:d2c4149d86c4dfceaff0e9c4eb5a5d42ca7815f81dd08baad4dc8bda6db2fb10",
                "Volumes": null,
                "WorkingDir": "/var/www/html",
                "Entrypoint": null,
                "OnBuild": [],
                "Labels": {}
            },
            "Architecture": "amd64",
            "Os": "linux",
            "Size": 491287784,
            "VirtualSize": 491287784,
            "GraphDriver": {
                "Name": "aufs",
                "Data": null
            }
        }
    ]
    

    It turns out the container-side directory /var/www needs to be empty, since it had a sub-directory www. Docker was not able to use it as a mounting point.

  • Philipp Schaffrath
    Philipp Schaffrath over 8 years
    Running docker run -v /c/Users/phisch/dev/htdocs:/var/www phisch:dev from within the docker machine (ssh session), i receive the same error message like before.
  • VonC
    VonC over 8 years
    @PhilippSchaffrath there seem to be two errors: the Container command not found or does not exist involve the CMD or ENTRYPOINT. If that command refers to a file which is supposed to be mounted, then we need to resolve the first. Does c:\Users\phisch\dev\htdocs exists? With that exact case?
  • Philipp Schaffrath
    Philipp Schaffrath over 8 years
    Yes this directory exists with the exact case. Those 2 messages always come together. Either everything works (when using no volume), or both messages are printed.
  • VonC
    VonC over 8 years
    @PhilippSchaffrath What is your ENTRYPOINT and CMD?
  • Philipp Schaffrath
    Philipp Schaffrath over 8 years
    Using docker-compose to mount volumes results in the exact same messages.
  • VonC
    VonC over 8 years
    @PhilippSchaffrath in an ssh session, can you cd to /c/Users/phisch/dev/htdocs?
  • Philipp Schaffrath
    Philipp Schaffrath over 8 years
    I have not set an ENTRYPOINT, and what do you mean by CMD?
  • VonC
    VonC over 8 years
    @PhilippSchaffrath CMD would be the default parameter passed to the ENTRYPOINT (which, by default, is sh -c): docs.docker.com/engine/reference/builder/#cmd
  • Philipp Schaffrath
    Philipp Schaffrath over 8 years
    Allright, neither CMD nor ENTRYPOINT are defined in my Dockerfile.
  • VonC
    VonC over 8 years
    @PhilippSchaffrath so... what your container is supposed to do when you do a docker run phisch:dev? Would (still in an ssh session) a docker run -v //c/Users/phisch/dev/htdocs:/var/www phisch:dev ls (note the final ls) work better?
  • Philipp Schaffrath
    Philipp Schaffrath over 8 years
    The image is supposed to provide apache and php for a local development environment. Running docker run -v //c/Users/phisch/dev/htdocs:/var/www phisch:dev ls through ssh prints the same error.
  • VonC
    VonC over 8 years
    @PhilippSchaffrath OK, can you do a docker inspect phisch:dev and tell me what the ENTRYPOINT and CMD are?
  • Philipp Schaffrath
    Philipp Schaffrath over 8 years
    Inspect gives me "Cmd": ["/bin/sh", "-c", "apt-get update"] and "Entrypoint": null for ContainerConfig and "Cmd": ["apache2-foreground"] and "Entrypoint": null for Config.
  • VonC
    VonC over 8 years
    @PhilippSchaffrath You can ignore ContainerConfig when inspecting an image. Is apache2-foreground visible in your container? Is it in your $PATH in a container bash session?
  • Philipp Schaffrath
    Philipp Schaffrath over 8 years
    It should be, but i am not sure how to validate it. I have included the inspect output in my original question.
  • VonC
    VonC over 8 years
    @PhilippSchaffrath do a docker run -v /c/Users/phisch/dev/htdocs:/var/www phisch:dev bash (no '//'): that should open a bash session in which you can do an echo $PATH.
  • Philipp Schaffrath
    Philipp Schaffrath over 8 years
    Running with the volume it gives me the same error like before. Without volume it does not start a bash session. C:\Users\phisch>docker run phisch:dev bash C:\Users\phisch>
  • VonC
    VonC over 8 years
    Try it in an ssh session
  • Philipp Schaffrath
    Philipp Schaffrath over 8 years
    I had to pass the parameters -t and -i to allocate a pseudo-tty and keep stdin. PATH contains /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin‌​.
  • VonC
    VonC over 8 years
    Well, can search for the apache2-foreground script? Is it in one of those paths?
  • Philipp Schaffrath
    Philipp Schaffrath over 8 years
    It is in /usr/local/bin and contains those lines: set -e, rm -f /var/run/apache2/apache2.pid, exec apache2 -DFOREGROUND.
  • VonC
    VonC over 8 years
    @PhilippSchaffrath that should work. Do you confirm you canot open a bash wile (from an ssh session) you are trying to mount an host folder? docker run -it --rm -v /c/Users/phisch/dev/htdocs:/var/www phisch:dev bash
  • Philipp Schaffrath
    Philipp Schaffrath over 8 years
    Yes, it fails with the same message.
  • VonC
    VonC over 8 years
    @PhilippSchaffrath When you do a docker run -it --rm hisch:dev bash, are able to do an ls /c/Users/phisch/dev/htdocs and ls /var/www ?
  • Philipp Schaffrath
    Philipp Schaffrath over 8 years
    /var/www of course is available. Oddly /c is not available. It is available on the linux vm that boot2docker creates, but not inside the docker container running on it.
  • Philipp Schaffrath
    Philipp Schaffrath over 8 years
    But this makes sense, since /c/Users/phisch/dev/htdocs needs to be on the host and in the boot2docker vm (which runs docker). And docker should mount it from the linux-host (boot2docker) to the docker container.
  • VonC
    VonC over 8 years
    @PhilippSchaffrath That is expected: inside the container, only /var/www is available, with the content of /c/... mounted in it.
  • VonC
    VonC over 8 years
    @PhilippSchaffrath So from docker, an ls /c/Users/phisch/dev/htdocs does work, does it not?
  • Philipp Schaffrath
    Philipp Schaffrath over 8 years
    From the boot2docker VM ls /c/Users/phisch/dev/htdocs works.
  • VonC
    VonC over 8 years
    When you do not mount that path, is there files in /var/www? (when doing a docker run -it --rm hisch:dev bash)
  • Philipp Schaffrath
    Philipp Schaffrath over 8 years
    Yes, there is one sub-directory in it. Might that be the problem?
  • Philipp Schaffrath
    Philipp Schaffrath over 8 years
    Actually this was the problem. When i mount an empty directory, it works just fine.
  • VonC
    VonC over 8 years
    @PhilippSchaffrath OK. I have included that conclusion in the answer for more visibility, but I have added a caveat.
  • Philipp Schaffrath
    Philipp Schaffrath over 8 years
    Thank you very much for your help, i appreciate it! I included the answer into my original post aswell.
  • Arno van Oordt
    Arno van Oordt over 2 years
    Instead of $PWD you can also just use "C:\path\to\my\host\folder" or "//c/path/to/my/host/folder" so this can work in other cli's than PowerShell as well :)
  • Johnny Oshika
    Johnny Oshika over 2 years
    @ArnovanOordt if you like absolute paths 😉
  • Pyetro
    Pyetro about 2 years
    In windows, a double slash is needed at the beginning of the path to indicate the working directory. Just to work with short path use like this docker run -v //$(PWD)/folder:/folder ...
  • VonC
    VonC about 2 years
    @Pyetro Good point, thank you. I have included your comment in the answer for more visibility.