【问题标题】:docker nginx - doen't go up when trying to add volumes for nginx.conf / default.confdocker nginx - 尝试为 nginx.conf / default.conf 添加卷时不会上升
【发布时间】:2020-02-26 16:37:38
【问题描述】:

当我尝试运行带有卷的 nginx 映像以复制/同步“nginx.conf”和“conf.d 文件夹”时,它不会启动。没有错误,但是如果我运行 docker container ls,则不会列出该容器。如果我评论“有问题的卷”,它确实会启动容器并且一切正常。我想指出,如果我将其中任何一个注释掉,容器不会启动,问题出在两个卷上。

文件夹结构:

Project Name
|-- core_dir
|-- core_dir
|-- core_file.file
|-- core_file.file
|-- docker-compose.yml
`-- docker
    |-- php
    |   |-- Dockerfile
    |   `-- php.ini
    |-- mysql
    `-- nginx
        |-- nginx.conf
        `-- conf.d
            `-- default.conf

有问题的卷:

volumes:
      - ./docker/nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./docker/nginx/conf.d/:/etc/nginx/conf.d

我的 docker-compose.yml:

version: "3.0"

services:

  #nginx
  nginx:
    image: nginx:stable-alpine
    ports:
      - "80:80"
    volumes:
      - ./:/var/www/html
      - ./docker/nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./docker/nginx/conf.d/:/etc/nginx/conf.d
    depends_on:
      - php
      - mysql
    networks:
      - wordpress


  #mysql
  mysql:
    image: mysql:5.7.22
    env_file:
      - .env.development
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE:
      MYSQL_USER: ${DB_USER}
      MYSQL_PASSWORD: ${DB_PASSWORD}
      MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
    networks:
      - wordpress
    volumes:
      - ./docker/mysql:/var/lib/mysql

  #php
  php:
    build:
      context: ./docker/php
      dockerfile: Dockerfile
    container_name: php
    volumes:
      - ./:/var/www/html
      - ./docker/php/php.ini:/usr/local/etc/php/php.ini
    ports:
      - "9000:9000"
    networks:
      - wordpress

networks:
  wordpress:

提前致谢,

布拉姆

【问题讨论】:

  • docker 主机上是否存在docker/nginx 文件夹?检查docker ps -a 以查看失败的容器并运行docker logs [container_id]。请分享您找到的日志
  • 错误日志显示如下:“daemon”指令在 /etc/nginx/nginx.conf 中重复:

标签: docker nginx docker-compose docker-volume volumes


【解决方案1】:

我已经解决了这个问题。

感谢rok 向我指出,我可以检查崩溃容器的日志,我能够查看导致崩溃的错误。

检查失败容器的日志

运行docker ps -a 以查看失败的容器,然后在失败的容器上运行docker logs [container_id]

错误

“daemon”指令在 /etc/nginx/nginx.conf 中重复:

Nginx.conf

 user  nginx;
    worker_processes  4;
    daemon off;

    error_log  /var/log/nginx/error.log warn;
    pid        /var/run/nginx.pid;

    events {
        worker_connections  1024;
    }

    http {
        include       /etc/nginx/mime.types;
        default_type  application/octet-stream;

        client_max_body_size 100M;
        server_names_hash_bucket_size 64;

        access_log  /var/log/nginx/access.log;
        # Switch logging to console out to view via Docker
        #access_log /dev/stdout;
        #error_log /dev/stderr;

        sendfile        on;
        keepalive_timeout  65;

        include /etc/nginx/conf.d/*.conf;
        #include /etc/nginx/sites-available/*.conf;
    }

解决方案

删除“daemon off;”行为我解决了这个问题。

【讨论】:

    【解决方案2】:

    容器失败的原因可能有很多:

    • 您挂载到容器的配置有问题
    • 您挂载的文件夹不会在主机上退出

    要了解问题所在,您需要运行 docker ps -a 以查看失败的容器,然后在失败的容器上运行 docker logs [container_id]

    另外,你可以关注nginx的docs,尝试只用Docker运行nginx。

    要排除卷的问题,您可以使用配置构建自定义 nginx 映像:

    FROM nginx
    RUN rm /etc/nginx/conf.d/default.conf
    COPY docker/nginx /etc/nginx
    

    docker build -t mynginx_image1 .

    docker run --name mynginx3 -p 80:80 -d mynginx_image1

    【讨论】:

      【解决方案3】:

      问题通常是在撰写文件中使用相对路径。

      您可以尝试使用绝对路径或命名卷而不是使用相对路径。

      【讨论】:

      • "daemon" 指令在 /etc/nginx/nginx.conf 中重复:
      • @BramJanssen - 您能否评论您可能在 nginx.conf 中添加的任何包含 *.conf 或删除守护程序指令。这应该有助于参考:link
      猜你喜欢
      • 1970-01-01
      • 2014-04-04
      • 2019-02-11
      • 2016-05-27
      • 2023-03-14
      • 2021-05-13
      • 2019-12-04
      • 2018-10-30
      • 2020-11-09
      相关资源
      最近更新 更多