背景
在自己的服务器上想通过 nginx 镜像创建容器,并挂载镜像自带的 nginx.conf 文件
docker run -it -d -v ~/nginx.conf:/etc/nginx/nginx.conf nginx
但是报错了
[root@poloyy ~]# docker run -it -d -v ~/nginx.conf:/etc/nginx/nginx.conf nginx e0e4b40446a64927603b85854c3a6472b2dfa5681fcbfa0e170c16b15e5c8fdd docker: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: process_linux.go:545: container init caused: rootfs_linux.go:76: mounting "/root/nginx.conf" to rootfs at "/etc/nginx/nginx.conf" caused: mount through procfd: not a directory: unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type. [root@poloyy ~]# client_loop: send disconnect: Broken pipe
提取关键错误信息
mounting "/root/nginx.conf" to rootfs at "/etc/nginx/nginx.conf" caused: mount through procfd: not a directory: unknown: Are you trying to mount a directory onto a file (or vice-versa)?
将“/root/nginx.conf”挂载到“/etc/nginx/nginx.conf”的rootfs导致:通过procfd挂载:不是目录:未知:您是否试图将目录挂载到文件上(反之亦然)
根因
- 不支持直接挂载文件,只能挂载文件夹
- 想要挂载文件,必须宿主机也要有对应的同名文件
解决方法
- 可以先不挂载 nginx.conf
- 先从容器中复制 nginx.conf 出来
- 然后可以自行修改 nginx.conf,自定义配置项
- 创建正式使用的 nginx 容器
从 test 容器中复制 nginx.conf 出来
当然也可以去网上随便找个 nginx.conf,最重要的是宿主机要有个 nginx.conf
docker run --name test -d nginx docker cp test:/etc/nginx/nginx.conf /data/
创建正式的 nginx 容器,挂载 nginx.conf 文件
可以赋予权限
docker run --privileged -it -p 80:80 -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro -v /data/nginx/conf/conf.d:/etc/nginx/conf.d:ro -v /data/nginx/html:/usr/share/nginx/html:rw -v /data/nginx/logs:/var/log/nginx -d nginx