使用自定义nginx配置文件启动nginx docker容器

时间:2020-03-30 12:32:27

标签: docker nginx

我的要求

步骤

我正在按照

的说明进行操作

我正在尝试创建一个容器(名称= mynginx1),以指定我自己的nginx conf文件

$ docker run --name mynginx1 -v C:/zNGINX/testnginx/conf:/etc/nginx:ro -P -d nginx

其中“ C:/ zNGINX / testnginx / conf”包含文件“ default.conf”,其内容为

server {
    listen 80;
    server_name  localhost;

    location / {
        proxy_pass http://localhost:3000;
    }

}

返回了容器ID,但“ docker ps”未显示其正在运行。

使用“ docker logs mynginx1”查看容器日志会显示以下错误

2020/03/30 12:27:18 [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)

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

我做的事情有2个错误

(1)在conf文件中,我使用的是“ proxy_pass http://localhost:3000;”
容器中的“ localhost”是容器主机,而不是我的计算机。因此,这需要更改为

proxy_pass http://host.docker.internal:3000;

(2)将配置文件复制到容器上的路径不正确,我需要添加“ conf.d”

docker run --name mynginx1 -v C:/zNGINX/testnginx/conf:/etc/nginx/conf.d:ro -P -d nginx

我正在阅读的文档(多个网站)没有提到在路径末尾添加“ conf.d”目录。但是,如果您查看“ /etc/nginx/nginx.conf”文件,则在最后一行会有提示

user  nginx;
worker_processes  1;

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;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

“包括/etc/nginx/conf.d/*.conf;”表示它将加载“ /etc/nginx/conf.d/”目录中以“ .conf”结尾的任何文件。

相关问题