简体   繁体   中英

Docker --volume does not mount folder into nginx container

I am trying to learn some docker commands ( without Dockerfile and I am on ubuntu). I use this command first:

docker container run -p 80:80 nginx:latest

Then inside my browser when I open http://192.168.99.100 ( which is my virtualmachine IP), it shows default page for nginx.

Then I stop the container and try this command:

dock container run --volume /home/mazoo/DockerExampleFolder/html:/usr/share/nginx/html -p 80:80 nginx:latest

I open in my brwoser http://192.168.99.100 and I receive 403 Forbidden error. My console shows:

2019/12/16 00:23:56 [error] 6#6: *1 directory index of "/usr/share/nginx/html/" is forbidden, client: 192.168.99.1, server: localhost, request: "GET / HTTP/1.1", host: "192.168.99.100"

Also I try http://192.168.99.100/1.html (1.html is a html file inside /home/mazoo/DockerExampleFolder/html folder on my computer which logically should be mounted into container) I receive 404 Not Found error in browser and in my console I see this error:

[error] 6#6: *1 open() "/usr/share/nginx/html/1.html" failed (2: No such file or directory), client: 192.168.99.1, server: localhost, request: "GET /1.html HTTP/1.1", host: "192.168.99.100"

Error is showing "/usr/share/nginx/html" did not recieve 1.html file from my host into container.

Then I try another path in my container:

docker container run --volume /home/mazoo/DockerExampleFolder/html:/var/html -p 80:80 nginx:latest

But when I browse http://192.168.99.100 it shows default nginx welcome page ( I have an index.html inside my html folder so I expect default welcome page should overriden) and again for http://192.168.99.100/1.html I receive Not Found error.

Seems for some reasons, my folder does not get mounted into my container.

First start the docker container up with a name:

dock container run --name nginx --volume /home/mazoo/DockerExampleFolder/html:/usr/share/nginx/html -p 80:80 nginx:latest

Then go inside the running container to explore.

docker exec -it nginx bash

Check for the expected files and ownerships in /usr/share/nginx/..

I suspect it's a permission issue.


Use a named volume:

docker rm nginx  ## clean up from earlier, if needed

docker volume create nginx-data

docker run --name nginx --rm -v nginx-data:/usr/share/nginx/html nginx:latest

outside the container:

cd /var/lib/docker/volumes/nginx-data/_data/

The answer is this:

Inside Oracle VM VirtualBox shared folder setting, my "home" was mapped as "hosthome" so instead of

dock container run --name nginx --volume /home/mazoo/DockerExampleFolder/html:/usr/share/nginx/html -p 80:80 nginx:latest

It should be

dock container run --name nginx --volume /hosthome/mazoo/DockerExampleFolder/html:/usr/share/nginx/html -p 80:80 nginx:latest

Means instead of "home" in the path, it should be "hosthome"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM