简体   繁体   English

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

[英]starting nginx docker container with custom nginx config file

My Requirements我的要求

Steps脚步

I am following instructions from我正在遵循来自

I'm trying to create a container (name = mynginx1) specifying my own nginx conf file我正在尝试创建一个容器(名称 = mynginx1),指定我自己的 nginx conf 文件

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

where "C:/zNGINX/testnginx/conf" contains the file "default.conf" and its contents are其中“C:/zNGINX/testnginx/conf”包含文件“default.conf”,其内容是

server {
    listen 80;
    server_name  localhost;

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

}

A container ID is returned, but "docker ps" does not show it running.返回容器 ID,但“docker ps”未显示它正在运行。

Viewing the container logs using "docker logs mynginx1" shows the following error使用“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)

What am I doing wrong?我究竟做错了什么?

There were 2 errors in what i was doing我在做什么有两个错误

(1) In the conf file, I was using "proxy_pass http://localhost:3000 ;" (1) 在 conf 文件中,我使用的是“proxy_pass http://localhost:3000 ;”
"localhost" in the container is the CONTAINER host, not MY computer.容器中的“localhost”是 CONTAINER 主机,而不是我的电脑。 Therefore this needed changing to因此,这需要更改为

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

(2) the path to copy my config file to on the container was not right, i needed to add "conf.d" (2) 复制我的配置文件到容器的路径不对,我需要添加“conf.d”

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

The documentation I was reading (multiple websites) did not mention adding the "conf.d" directory on the end of the path.我正在阅读的文档(多个网站)没有提到在路径末尾添加“conf.d”目录。 However if you view the "/etc/nginx/nginx.conf" file there is a clue on the last line但是,如果您查看“/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;
}

The "include /etc/nginx/conf.d/*.conf;" “包含/etc/nginx/conf.d/*.conf;” indicates that it loads any file ended in ".conf" from the "/etc/nginx/conf.d/" directory.表示它从“/etc/nginx/conf.d/”目录加载任何以“.conf”结尾的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM