背景
在自己的伺服器上想通過 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