Nginx docker 仅在 docker-compose 上找到容器

Nginx docker only finds containers on docker-compose

我不确定这是否正确,但我在容器上有一个 nginx 代理对 Nodejs 应用程序的请求,并且在 [=24= 上一切正常] 文件取决于功能和所有这些。但是现在我想将它们分成不同的 docker 组合文件然后碰巧 nginx 找不到 upstream nodejsapp:

2017/11/10 15:21:38 [emerg] 1#1: host not found in upstream "nodejsapp" in /etc/nginx/conf.d/default.nodejsapp.conf:8
nginx: [emerg] host not found in upstream "nodejsapp" in /etc/nginx/conf.d/default.nodejsapp.conf:8

我的代理配置有效,但在任何情况下都是:

server {
    listen       80;

    location / {
        proxy_pass http://nodejsapp:4000;
        proxy_set_header X-Remote-Addr $proxy_add_x_forwarded_for;
        proxy_cache STATIC;
        proxy_cache_valid 200 1d;
        proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
        proxy_ignore_headers X-Accel-Expires Expires Cache-Control;
    }

    location ~* ^.+\.(atom|bmp|bz2|css|doc|docx|eot|exe|gif|gz|ico|jpeg|jpg|js|mid|midi|mp4|ogg|ogv|otf|pdf|png|ppt|pptx|rar|rss|rtf|svg|svgz|swf|tar|tgz|ttf|txt|wav|woff|xls|xml|zip)$ {
        access_log  off;
        log_not_found   on;
        expires     max;
        proxy_pass  http://nodejsapp:4000;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

如果我这样做 docker ps 我明白了:

CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS                          PORTS                                                NAMES
f4d888e6c37d        nginx   "nginx -g 'daemon ..."   3 minutes ago       Restarting (1) 33 seconds ago                                                        nginx
5867ac82e5da        nodejsapp:3.2.0-dev     "pm2-docker proces..."   25 minutes ago      Up 25 minutes                   80/tcp, 443/tcp, 43554/tcp, 0.0.0.0:4000->4000/tcp   nodejsapp

我了解到 docker 容器可以通过名称访问其他容器。然后我检查了我拥有的网络:docker network ls

NETWORK ID          NAME                DRIVER              SCOPE
cf9b7ea0a5b7        bridge              bridge              local
549c48fa592a        docker_default      bridge              local
652ffb4094f0        host                host                local
412ed3bbfd01        nginx_default       bridge              local
85a803c70f83        none                null                local

然后我 docker network inspect bridge 我可以看到 nodejsapp 容器等:

"Containers": {
            "5867ac82e5dad7642155a7c3df05c37cd83c1be5a0eb49d55cf5325bfaa7ea4d": {
                "Name": "nodejsapp",
                "EndpointID": "a362d02b083e90bb0acc50a2af8ec5a5f0a9a23a9f0ed2bfb62b1a6c60586fb8",
                "MacAddress": "02:42:ac:11:00:04",
                "IPv4Address": "172.17.0.4/16",
                "IPv6Address": ""
            },
            "b272cd07cc1e5c28632cbaf05858cf373b6a13304f69bee5de73fc57e5a3cf79": {
                "Name": "sad_poitras",
                "EndpointID": "c5b1e2389f5da8b9acf4f6c7a13f5d6e09411cd960b9e4dcd8ed38fef2982780",
                "MacAddress": "02:42:ac:11:00:03",
                "IPv4Address": "172.17.0.3/16",
                "IPv6Address": ""
            },
            "ffd171939e534a4749a4d215f52c1add353537e91e4a95d7b752775ee2b4c70f": {
                "Name": "elated_hawking",
                "EndpointID": "eca2709e189d8d5922e1dd5d8c5af14826bd77d5a399b63363e3b07671fb5169",
                "MacAddress": "02:42:ac:11:00:02",
                "IPv4Address": "172.17.0.2/16",
                "IPv6Address": ""
            }
        },

而且我可以看到 nginx 没有连接到任何网络,但在我看来它试图创建自己的网络 (nginx_default)?请注意,我看不到任何连接到该网络的容器,可能是因为它在启动时失败:

[
    {
        "Name": "nginx_default",
        "Id": "412ed3bbfd01336c86473a8d07fe6c444389cac44838552a40d6b7ec7f4c972d",
        "Created": "2017-11-10T15:21:35.2016921Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.19.0.0/16",
                    "Gateway": "172.19.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]

有没有办法使用桥接器获取它?我是不是做错了什么?

更新:我已经将 nodejsapp 设置在网络上 nginx_default 然后我 nginx 开始了。但是代理根本不工作,我得到 nginx 的主屏幕。我 docker 将其拆分为两个不同的撰写文件后的撰写文件:

version: '2'
services:
  nginx:
    build:
      context: .
      dockerfile: Dockerfile.dev
    image: nginx:3.1.0-dev
    ports:
     - "10100:80"
    restart: always
    container_name: nginx

我的Dockerfile.dev:

FROM nginx:latest
RUN  mkdir /tmp/cache
COPY ./nodejsapp/default.dev.nodejsapp.conf /etc/nginx/conf.d/default.nodejsapp.conf
COPY nginx.dev.conf /etc/nginx/nginx.conf

另一个docker-撰写:

version: '2'
services:
  nodejsapp:
    build:
      context: .
      dockerfile: Dockerfile.dev
    image: nodejsapp:3.2.0-dev
    ports:
     - "4000:4000"
    restart: always
    container_name: nodejsapp

和对应的Dockerfile.dev:

FROM keymetrics/pm2:latest
COPY dist dist/
COPY process.yml .
CMD ["pm2-docker", "process.yml"]
如果您创建 2 个 docker-compose 文件,

Docker 会创建 2 个不同的隔离网络。 AFAIK,唯一的方法是创建一个网络并将 "link" 容器连接到该网络。

所以,创建一个网络然后使用它。请在此处查看文档:https://docs.docker.com/compose/networking/#use-a-pre-existing-network

您可以使用外部网络连接两个容器。但首先您必须创建网络,我们将其命名为 my-network:

docker network create my-network

然后我们需要在每个docker-compose文件中声明网络:

networks:
  default:
    external:
      name: my-network