首页 > 解决方案 > Docker:如果我在 docker-compose.yml 中声明卷,Nginx 不会运行

问题描述

我正在尝试使用 docker-compose 在 docker 中将 nginx 作为容器运行,但不幸的是,我无法正确运行它。

这是我的 docker-compose.yml:

version: '3'
services:
 webserver:
  container_name: webserver
  hostname: webserver
  image: nginx
  ports:
   - 80:80
   - 443:443
  volumes:
   - ./nginx:/etc/nginx

这是错误:

/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/01/18 19:04:26 [emerg] 1#1: open() "/etc/nginx/nginx.conf" failed (2: No such file or directory)
nginx: [emerg] open() "/etc/nginx/nginx.conf" failed (2: No such file or directory)

我在卷中使用了相对路径和绝对路径,但它们都不起作用。如果我在主机上有可用的目录,它将无法工作。如果我在主机中没有该目录,当我运行 docker-compose up 时,它会在主机中为 nginx 创建一个空目录,但它会留空。

任何想法我的设置有什么问题?

谢谢你。

标签: dockernginxdocker-compose

解决方案


不,不要尝试在容器本身中手动修改所有配置文件。

Nginx 有/etc/nginx/conf.d这个功能,所以将你的海关 confs 安装在里面。

例子:

您当前的目录应该如下所示:

.
├── conf
│   └── custom.conf
├── docker-compose.yml
└── html
    └── index.html

码头工人-compose.yml

services:
  nginx:
    image: nginx:latest
    ports:
      - 80:80
    volumes:
      - ./conf:/etc/nginx/conf.d # custom conf goes here
      - ./html:/tmp              # custom html goes here

我只是将 html 放在“/tmp”中,以向您展示我的自定义配置有效..

./conf/custom.conf

server {
    listen     80;
    location / {
            root /tmp/;
            index  index.html index.htm;
    }
}

./html/index.html

<h1>nginx custom conf</h1>

然后

$ docker-compose up -d
Creating network "nginx_default" with the default driver
Creating nginx_nginx_1 ... done

$ curl localhost
<h1>hello</h1>

推荐阅读