用于 nginx 和 nodejs 服务器的 docker-compose

dag*_*da1 11 nginx node.js docker docker-compose

我有以下docker-compose.yml文件:

version: '3'
services:
    server:
        build:
            context: ../../
            dockerfile: ./packages/website/Dockerfile
        command: node /cutting/index.js
        environment:
            PORT: 8000
            NODE_ENV: production
        restart: always
    nginx:
        build:
            context: ./
            dockerfile: ./nginx/Dockerfile
        command: tail -f /dev/null
        depends_on:
            - server
        ports:
            - "8000:8000"
        restart: always
Run Code Online (Sandbox Code Playgroud)

以及以下内容nginx.conf

worker_processes 1;

user nobody nogroup;
pid /tmp/nginx.pid;
error_log /tmp/logs/nginx.error.log;

events {
    worker_connections 1024;
    accept_mutex off;
}

http {
    include mime.types;
    default_type application/octet-stream;
    access_log /tmp/logs/nginx.access.log combined;
    sendfile on;

    server {
        proxy_http_version 1.1; # this is essential for chunked responses to work

        listen   80; ## listen for ipv4; this line is default and implied
        listen   [::]:80 default ipv6only=on; ## listen for ipv6
        client_max_body_size 4G;
        server_name frontend;

        gzip on;
        gzip_disable "msie6";
        gzip_vary on;
        gzip_proxied any;
        gzip_comp_level 6;
        gzip_buffers 16 8k;
        gzip_http_version 1.1;
        gzip_types application/javascript text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        keepalive_timeout 5;

        location /static/  {
            alias /static/;
        }

        location  / {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;

            # UNCOMMENT LINE BELOW IF THIS IS BEHIND A SSL PROXY
            #proxy_set_header X-Forwarded-Proto https;

            proxy_redirect off;
            proxy_pass   http://localhost:8000;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果这会创建 2 个容器,我怎样才能让 nginx 看到另一个容器上的 localhost:8000?

Mic*_*ton 15

您可以name按照您在docker-compose.yml. Docker 通过每个容器内的 DNS 为命名容器提供 IP 地址,并在容器更新时更新它们。

在这种情况下,您将其称为server,因此您将使用它。

    proxy_pass http://server:8000;
Run Code Online (Sandbox Code Playgroud)

文档:Compose 中的网络