Linux VPS上Docker挂载nginx.conf至容器失败求助
Docker Nginx容器配置文件挂载失败排查与解决
环境信息
- 服务器:Ubuntu 23.04 VPS
- 问题仅出现在Linux VPS,Windows开发环境无异常
问题详情
执行docker-compose up启动容器时,Nginx容器启动失败,报错如下:
root@srv502529:/docker_projects/personal-site-2024# docker-compose up Creating network "personal-site-2024_default" with the default driver Creating personal-site-2024_api_proxy_server_1 ... done Creating personal-site-2024_webserver_1 ... done Creating personal-site-2024_cms_server_1 ... done Creating personal-site-2024_nginx_1 ... error ERROR: for personal-site-2024_nginx_1 Cannot start service nginx: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: error mounting "/nginx.conf" to rootfs at "/etc/nginx/nginx.conf": mount /nginx.conf:/etc/nginx/nginx.conf (via /proc/self/fd/6), flags: 0x5000: 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 ERROR: for nginx Cannot start service nginx: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: error mounting "/nginx.conf" to rootfs at "/etc/nginx/nginx.conf": mount /nginx.conf:/etc/nginx/nginx.conf (via /proc/self/fd/6), flags: 0x5000: 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
确认nginx.conf文件存在于项目目录中,但挂载仍失败。
相关docker-compose配置片段
services: nginx: image: nginx:alpine ports: - "80:80" volumes: - /nginx.conf:/etc/nginx/nginx.conf depends_on: - webserver - api_proxy_server - cms_server
已尝试操作及新问题
尝试修改卷映射为./nginx:/etc/,容器启动成功,但Nginx报错找不到配置文件:
nginx_1 | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration nginx_1 | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ nginx_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh nginx_1 | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist nginx_1 | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh nginx_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh nginx_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh nginx_1 | /docker-entrypoint.sh: Configuration complete; ready for start up nginx_1 | 2024/04/08 15:35:46 [emerg] 1#1: open() "/etc/nginx/nginx.conf" failed (2: No such file or directory) nginx_1 | nginx: [emerg] open() "/etc/nginx/nginx.conf" failed (2: No such file or directory)
解决方案
1. 修正主机文件路径
原配置中/nginx.conf是绝对路径,指向服务器根目录下的文件,而非项目目录中的文件。需改为相对路径(确保执行docker-compose up时处于项目根目录):
volumes: - ./nginx.conf:/etc/nginx/nginx.conf
./nginx.conf表示当前工作目录下的nginx.conf文件,与容器内/etc/nginx/nginx.conf文件绑定。
2. 验证路径正确性
执行以下命令确认文件路径和权限:
# 确认文件存在 ls -l ./nginx.conf # 确保文件权限允许读取 chmod 644 ./nginx.conf
3. 避免覆盖容器关键目录
之前使用./nginx:/etc/会覆盖容器整个/etc目录,导致Nginx丢失原有系统配置文件。正确做法是仅挂载需要自定义的文件或子目录,比如:
- 挂载自定义主配置:
./nginx.conf:/etc/nginx/nginx.conf - 挂载额外配置目录:
./conf.d:/etc/nginx/conf.d
4. 重新启动容器
修改配置后,重启容器:
docker-compose down docker-compose up -d
内容的提问来源于stack exchange,提问作者Hayden Gorringe
相关产品推荐
相关产品推荐

