Docker compose - share volume Nginx

2019-06-16 19:40发布

问题:

I just want to test Docker and it seems something is not working as it should. When I have my docker-compose.yml like this:

web:
  image: nginx:latest
  ports:
    - "80:80"

when in browser I run my docker.app domain (sample domain pointed to docker IP) I'm getting default nginx webpage.

But when I try to do something like this:

web:
  image: nginx:latest
  volumes:
    - /d/Dev/docker/nginx-www/nginx/html/:/usr/share/nginx/html/ 
  ports:
    - "80:80"

when I run:

docker-compose up -id

when I run same url in browser I'm getting:

403 Forbidden

nginx/1.9.12

I'm using Windows 8.1 as my host.

Do I do something wrong or maybe folders cannot be shared this way?

EDIT

Solution (based on @HemersonVarela answer):

The volume I've tried to pass was in D:\Dev\docker location so I was using /d/Dev/docker at the beginning of my path. But looking at https://docs.docker.com/engine/userguide/containers/dockervolumes/ you can read:

If you are using Docker Machine on Mac or Windows, your Docker daemon has only limited access to your OS X or Windows filesystem. Docker Machine tries to auto-share your /Users (OS X) or C:\Users (Windows) directory.

so what I needed to do, is to create my nginx-ww/nginx/html directory in C:\users\marcin directory, so I ended with:

web:
  image: nginx:latest
  volumes:
    - /c/Users/marcin/docker/nginx-www/nginx/html/:/usr/share/nginx/html/ 
  ports:
    - "80:80"

and this is working without a problem. Files are now shared as they should be

回答1:

If you are using Docker Machine on Windows, docker has limited access to your Windows filesystem. By default Docker Machine tries to auto-share your C:\Users (Windows) directory.

So the folder .../Dev/docker/nginx-www/nginx/html/ must be located somewhere under C:\Users directory in the host.

All other paths come from your virtual machine’s filesystem, so if you want to make some other host folder available for sharing, you need to do additional work. In the case of VirtualBox you need to make the host folder available as a shared folder in VirtualBox.



回答2:

You have to set a command to copy your nginx.conf into the nginx container:

Dockerfile:

FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf`

Creat a dir name it nginx and put the Dockerfile & nginx.conf there, then you have to set a build:

docker-compose.yml:

web:
    image: nginx:latest 
    build :./nginx/
volumes:
    - /d/Dev/docker/nginx-www/nginx/html/:/usr/share/nginx/html/ 
ports:
    - "80:80"

Then build your containers with : sudo docker-compose build