skip to Main Content

Using Docker with -v "c:mydir:/mydir" or with -v "c:/mydir:/mydir" correctly mounts the directory C:mydir inside my container.

However, I once omitted the drive letter by mistake and did something like:

docker run -it –rm -v "/mydir:/mydir" bash

The files saved to /mydir inside the container didn’t appear in the Windows directory C:mydir. They reappear inside other Docker containers if I again mount "/mydir" without the drive letter. I cannot find these files anywhere on my hard drive.

So exactly which Windows directory got mounted inside the container when I omitted the drive letter in the example above?

Thanks…

2

Answers


  1. First off, I didn’t realize this "feature" existed. You probably also saw that the docs say,

    When the host directory of a bind-mounted volume doesn’t exist, Docker automatically creates this directory on the host for you. In the example above (docker run -v /doesnt/exist:/foo -w /foo -i -t ubuntu bash), Docker creates the /doesnt/exist folder before starting your container.

    I was able to confirm that on Linux using docker run --rm -it -v /foo:/foo ... does actually create /foo which is … surprising and potentially disastrous. (This is another reason to be selective about which users can run Docker commands and how.)

    Anyways, I tried the same thing on Windows 10 using Git Bash and, I’m guessing because I wasn’t running the command as an administrator or a privileged user, docker complains that access is denied when it attempts to create the directory at C:Program FilesGitfoo.

    However, if you’re not using Git Bash, that may not directly answer your question but should be something to go on. I’d expect the newly directory to be somewhere within Program Files (e.g. powershell).

    UPDATE:
    After some more digging and trying myself using PowerShell, I’ve determined that the files actually live within the Docker VM that Windows uses.

    I was able to start a new container (using the nsenter1 image) which pokes into this VM and find the files using the following:

    docker run -it --rm --privileged --pid=host justincormack/nsenter1
    # now inside container
    find . -name foo
    

    And, in my case, the files were located (again, in the VM via the container) at /containers/services/01-docker/rootfs/foo

    See this forum post for more details.

    Login or Signup to reply.
  2. Docker volumes on Windows are always created in the path of the graph driver, which is where Docker stores all image layers, writeable container layers and volumes. By default the root of the graph driver in Windows is

    \wsl$docker-desktop-datadatadockervolumes
    

    but you can mount a volume to a specific directory when you run a container. You can copy paste the above path in windows explorer & check it out.

    However, if you want to mount C:mydir inside the docker container than you could do it either by,

    docker run -it --rm -v //c/mydir:/mydir bash
    

    or

    docker run -it --rm -v /c/mydir:/mydir bash
    

    Alternaively, you could also go to the directory(in this case C:mydir) & try,

    docker run -it --rm -v //$(PWD)/mydir:/mydir bash
    

    For more info, you can read the official documentation here.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search