问题:
使用docker 挂在nginx配置文件镜像,挂载数据卷失败并且容器直接退出
解决:
这里先说解决办法:
- 首先启动一个nginx容器,然后使用cp命令拷贝一份配置文件到宿主机,然后删除容器。我是整个nginx一起拷贝,也可以拷贝单个配置文件
docker cp 3516d6423546:/etc/nginx /etc/nginx
- 然后重新启动容器,并挂载镜像
docker run -d -v /etc/nginx/nginx/:/etc/nginx nginx
- 此时正常启动,nginx正常运行,数据卷也是正常的
原因:
下面说一下原因:开始由于本地宿主机没有配置文件,挂载的时候,直接启动nginx容器,容器会判断配置文件,发现没有conf配置文件,故启动容器失败从而挂载数据卷也失败。
回到docker hub 看nginx有一段:
Complex configuration
$ docker run --name my-custom-nginx-container -v /host/path/nginx.conf:/etc/nginx/nginx.conf:ro -d nginx
For information on the syntax of the nginx configuration files, see the official documentation (specifically the Beginner's Guide).
If you wish to adapt the default configuration, use something like the following to copy it from a running nginx container:
$ docker run --name tmp-nginx-container -d nginx
$ docker cp tmp-nginx-container:/etc/nginx/nginx.conf /host/path/nginx.conf
$ docker rm -f tmp-nginx-container
结合实际来看,如果要修改配置,可以先拷贝一份配置到宿主机,然后挂载数据卷实现配置实时共享。