简体   繁体   English

Docker --volume 不会将文件夹挂载到 nginx 容器中

[英]Docker --volume does not mount folder into nginx container

I am trying to learn some docker commands ( without Dockerfile and I am on ubuntu).我正在尝试学习一些 docker 命令(没有 Dockerfile,我在 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.然后在我的浏览器中,当我打开http://192.168.99.100 (这是我的虚拟机 IP)时,它显示了 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.我在我的 brwoser http://192.168.99.100 中打开并收到 403 Forbidden 错误。 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:我也尝试http://192.168.99.100/1.html(1.html是我计算机上 /home/mazoo/DockerExampleFolder/html 文件夹中的一个 html 文件,逻辑上应该安装到容器中)我收到 404 Not Found 错误浏览器并在我的控制台中看到此错误:

[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.错误显示“/usr/share/nginx/html”没有从我的主机接收到 1.html 文件到容器中。

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.但是当我浏览http://192.168.99.100 时,它显示默认的 nginx 欢迎页面(我的 html 文件夹中有一个 index.html,所以我希望默认的欢迎页面应该被覆盖),然后再次显示http://192.168.99.100/1。 html我收到未找到错误。

Seems for some reasons, my folder does not get mounted into my container.似乎由于某些原因,我的文件夹没有安装到我的容器中。

First start the docker container up with a name:首先使用名称启动 docker 容器:

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/..检查 /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在 Oracle VM VirtualBox 共享文件夹设置中,我的“home”被映射为“hosthome”,而不是

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"意思是路径中的“home”而不是“hosthome”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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