You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法通过Docker Compose创建Nginx容器的问题排查求助

Nginx Docker Compose 容器启动失败问题解决

问题描述

Docker Compose配置

nginx:
  container_name: nginx
  image: <private-repo>/nginx:latest
  volumes:
    - ./myfile.conf:/etc/nginx/conf.d/default.conf
    - ./nginx-ssl:/etc/nginx/ssl
    - mydir-static:/usr/share/html
  ports:
    - "443:443"
  networks:
    - my-network

启动错误信息

⠿ Container nginx                    Starting                                                                                               0.9s
Error response from daemon: 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 
"/var/lib/mydeploymentfolder/deployment/myfile.conf" to rootfs at "/etc/nginx/conf.d/default.conf": mount /var/lib/mydeploymentfolder/deployment/myfile.conf:/etc/nginx/conf.d/default.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配置文件(myfile.conf)

upstream my_service {
    server myservice:8086;
}

server {
    listen              443 ssl;
    server_name xxx.xxx.xxx.org xx.xxx.xx.xxx xx.xx.xx.xx localhost;
    server_tokens off;

    ssl_certificate      /etc/nginx/ssl/myfile.crt;
    ssl_certificate_key  /etc/nginx/ssl/myfile.key;

    client_max_body_size 4G;
    add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains; preload';
    add_header X-XSS-Protection "1";
    add_header X-Content-Type-Options nosniff;

    location /static {
        alias /usr/share/html;
    }

    location /ws {
        proxy_pass http://my_service;
        proxy_http_version 1.1;
        proxy_read_timeout 300;
        proxy_redirect     off;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host $server_name;
    }

    location / {
        proxy_pass http://my_service;
        proxy_set_header X-Forwarded-Host $server_name;
        proxy_set_header X-Real-IP $remote_addr;
        add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
        proxy_read_timeout 300;
    }
}

问题原因

错误核心是文件/目录类型不匹配的挂载错误,具体可能为以下情况:

  • 宿主机上的./myfile.conf不是文件:要么不存在,要么被误创建成了目录
  • 容器镜像内的/etc/nginx/conf.d/default.conf是目录而非文件(部分自定义Nginx镜像可能会将该路径设为目录)
  • Docker Compose执行时的工作目录错误,导致相对路径./myfile.conf指向了不存在或错误的位置

解决方案

1. 检查并修复宿主机文件

执行命令确认文件状态:

ls -l ./myfile.conf
  • 如果文件不存在:将上述myfile.conf内容写入该文件
  • 如果是目录:删除目录后重新创建文件
    rm -rf ./myfile.conf
    # 写入配置内容
    cat > ./myfile.conf <<EOF
    upstream my_service {
        server myservice:8086;
    }
    
    server {
        listen              443 ssl;
        server_name xxx.xxx.xxx.org xx.xxx.xx.xxx xx.xx.xx.xx localhost;
        server_tokens off;
    
        ssl_certificate      /etc/nginx/ssl/myfile.crt;
        ssl_certificate_key  /etc/nginx/ssl/myfile.key;
    
        client_max_body_size 4G;
        add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains; preload';
        add_header X-XSS-Protection "1";
        add_header X-Content-Type-Options nosniff;
    
        location /static {
            alias /usr/share/html;
        }
    
        location /ws {
            proxy_pass http://my_service;
            proxy_http_version 1.1;
            proxy_read_timeout 300;
            proxy_redirect     off;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Host $server_name;
        }
    
        location / {
            proxy_pass http://my_service;
            proxy_set_header X-Forwarded-Host $server_name;
            proxy_set_header X-Real-IP $remote_addr;
            add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
            proxy_read_timeout 300;
        }
    }
    EOF
    

2. 验证容器镜像内的目标路径

临时启动镜像查看conf.d目录结构:

docker run --rm <private-repo>/nginx:latest ls -l /etc/nginx/conf.d/

如果发现default.conf是目录,修改Compose挂载路径为conf.d下的新文件(Nginx会自动加载所有.conf后缀文件):

nginx:
  container_name: nginx
  image: <private-repo>/nginx:latest
  volumes:
    - ./myfile.conf:/etc/nginx/conf.d/my_custom.conf  # 修改此处路径
    - ./nginx-ssl:/etc/nginx/ssl
    - mydir-static:/usr/share/html
  ports:
    - "443:443"
  networks:
    - my-network

3. 使用绝对路径挂载(避免工作目录问题)

如果不确定当前工作目录是否正确,直接使用宿主机上myfile.conf的绝对路径:

volumes:
  - /absolute/path/to/your/myfile.conf:/etc/nginx/conf.d/default.conf
  # 其他挂载保持不变

内容的提问来源于stack exchange,提问作者noob_coder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 01:36:10