compose icon indicating copy to clipboard operation
compose copied to clipboard

Windows volume mapping works only from home directory

Open tooda02 opened this issue 10 years ago • 29 comments

In Windows, the Getting Started example works correctly only if the working directory is under the user's home directory (C:\users\username). Otherwise, it fails with the message:

python: can't open file 'app.py': [Errno 2] No such file or directory

which occurs because the /code directory is empty in the container.

The example Dockerfile is:

FROM python:2.7
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
CMD python app.py

and docker-compose.yml contains:

volumes:
  - .:/code

A standard Docker build produces a working container (that is, /code/app.py exists) regardless of the working directory. However, docker-compose up creates a working image only if run from C:\users\username or one of its subdirectories.

tooda02 avatar Dec 15 '15 13:12 tooda02

Yes, this is because Docker Machine only mounts that one shared folder by default. There's nothing we can do about this in Compose.

dnephin avatar Dec 15 '15 15:12 dnephin

@dnephin If I take docker-compose out of the picture by running:

docker build -t web .
docker run web

the container starts without error and I can exec a bash shell that shows the directory:

image

Note that the example above is running outside the home directory. So there is something about the interface between docker-compose and docker-machine that's causing this error.

tooda02 avatar Dec 15 '15 15:12 tooda02

There is no volume in that example. If you were to use docker run -v $PWD:/code you'd have the same problem.

You can remove the volumes: ... from the compose file to get it working with compose as well.

dnephin avatar Dec 15 '15 15:12 dnephin

Thanks! You are correct - removing volume makes it work. So I'd suggest that you update the Getting Started example to eliminate the Volume specification (or at the least, include a warning about this problem).

tooda02 avatar Dec 15 '15 15:12 tooda02

The volume config is important, as it lets you edit the code and see changes immediately, but we don't actually demonstrate that.

There was a suggestion long ago to add the volume config in a separate step after getting the app running, and demonstrating the benefits. I still think that'd be a good change, and you're right that we should also warn about mounting issues.

aanand avatar Dec 15 '15 16:12 aanand

Separate step would be nice. That's what I ended up doing in a project that uses docker-compose for local dev environment, since not everyone has similar mountings but at least they can get the thing running immediately and if/when they need the live updates, they can see step n+1.

mikkotikkanen avatar Feb 02 '16 08:02 mikkotikkanen

So there is no way around it? I tried mounting manually directory on other partition:

docker-machine.exe stop default
VBoxManage.exe sharedfolder add default --name "d/projects" --hostpath "D:\projects"
VBoxManage.exe showvminfo default | grep -i share -A4
docker-machine.exe start default
docker-compose up -d
// did not work
docker run -it -v "//d/projects:/d/projects" nginx /bin/sh
// did work to some extent

Hoping docker-compose will pick up on that automatically. But not only he didn't but also docker machine did mount only like 1 directory in projects ( the directory with docker files and also not all of them )

There isn't a way to make it work?

tarach avatar Feb 16 '16 06:02 tarach

have you shared the drive containing the project directory in the docker settings? (see shared drive section here)

Once I'd done that I was able to run the Getting Started tutorial fine.

jennet avatar Jul 07 '16 09:07 jennet

Here's a workaround for volumes outside of C:\Users. I'm on Win8.1, Docker Machine 0.7.0, no git bash. Add a shared folder and mount it inside the VM:

cd d:\my-project
vboxmanage sharedfolder add default --name my-project --hostpath "%cd%" --transient

docker-machine ssh default "sudo mkdir -p /d/my-project"
docker-machine ssh default "sudo mount -t vboxsf -o defaults,uid=`id -u docker`,gid=`id -g docker` my-project /d/my-project"

docker-compose up

Notes:

  • Add --readonly to the vboxmanage sharedfolder command to create a read-only share
  • If vboxmanage is not in your PATH, try "%VBOX_MSI_INSTALL_PATH%\vboxmanage"
  • Replace default with %DOCKER_MACHINE_NAME% if necessary
  • You have to do these steps for every project

vweevers avatar Jul 13 '16 15:07 vweevers

You might want to give the Docker for Windows beta a try - it uses a different approach for file syncing and doesn't have the C:\Users restriction.

aanand avatar Jul 13 '16 16:07 aanand

Made a tool for it: docker-share. Give it a spin, when Docker for Windows is not an option for you. In its simplest form, the workflow is:

docker-machine start
@FOR /f "tokens=*" %i IN ('docker-machine env') DO @%i

cd my-project
docker-share mount -t
docker-compose up

vweevers avatar Jul 15 '16 10:07 vweevers

I'm also having this problem in Windows 10. I'm running Docker for Windows (latest, 1.12.0-rc4-beta19) to no avail. I can ADD files but I cannot mount volumes. My project is in a subdirectory under C:\Users\me\Whatever\DockerProject. Would love to see this resolved!

wulftone avatar Jul 17 '16 16:07 wulftone

@wulftone I am using Windows 10 too. I executed "docker inspect" and found the volume was mounted but the error still came out. Finally I resolved by going to settings and set set Shared Drives. I hope it can help! image

klawhk avatar Aug 02 '16 03:08 klawhk

As I also still see these problems with devs using Windows, I came over https://forums.docker.com/t/volume-mounts-in-windows-does-not-work/10693/105 and it seems this still is a problem and only working for some users, for others it does work after fiddling around with firewall, security policies etc and for others it just doesnt work

philicious avatar Aug 04 '16 23:08 philicious

@klawhk Thank you. That solved it for me. :)

enjikaka avatar Aug 16 '16 14:08 enjikaka

oh a little update: for me the problems were because of a firewall integrated in AV (AntiVirus), so shut that OFF. Windows Firewall itself doesnt seem to cause problems tho.

philicious avatar Aug 16 '16 14:08 philicious

The above workaround comment by @vweevers works great. When in doubt use docker inspect to find the mounted folders. I just mounted the entire project folder and that works fine.

barrystaes avatar Oct 19 '16 14:10 barrystaes

thank you @vweevers , great tool.

ztwo79 avatar Oct 20 '16 01:10 ztwo79

by me it still automounts c:\users even though it is says in shared drives c how does one convince it to share c:\

markl17 avatar Feb 15 '17 17:02 markl17

I think this issue could be much more simply resolved by some more error handling in the docker-compose up command. If there was a way to inform the user (I guess info vs error) that the volume mount in windows using docker toolbox did not work because folder xyz does not exist (and then display where docker-compose is looking) would solve 99% of the problem. I know that much of my time was spent thinking it was something with the container I was using rather than not using my c:\Users\ directory even after I read the documentation. If I had seen in the console log that the mounting was from c:\Users I probably would have worked forward from there...or even to update the shared folders in VirtualBox.

Lampei avatar Jun 11 '17 15:06 Lampei