NGINX 指向不同 docker 组成容器

NGINX pointing different docker compose containers

我是 docker 的新手,我 运行 遇到了架构问题: 我的计算机中有两组容器(CUSTOMER1、CUSTOMER2)运行,由相同的 docker-compose 文件组成,它们都包含两个相同的应用程序(APP1、APP2)。

我的问题是:有没有办法,在同一主机中使用nginx服务器访问所需容器中的所需应用程序(例如:APP1 通过浏览器进入 CUSTOMER1 容器 link(例如:CUSTOMER1.MYPC.IT/APP1)通过我的计算机 使用 Windows 个容器?

为了更好的解释,我附上一张图:

image showing an host called "my_computer" which is running two different container, both with two applications running (APP1, APP2) with an external nginx server

这里是来自 nginx 容器的 nginx.conf 文件:

worker_processes  auto;
events {
    worker_connections  4096;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  99999;

     server {
            listen 80;
            server_name localhost 127.0.0.1;
            resolver 127.0.0.11;
            location /APP1 {
                proxy_pass          http://test_app1/......;
                proxy_set_header    X-Forwarded-For $remote_addr; 
                proxy_read_timeout 300;
                proxy_connect_timeout 300;
                proxy_send_timeout 300;
            }

            location /APP2 {
                include  /etc/nginx/mime.types;
                proxy_pass          http://test_app2/......;
                proxy_set_header    X-Forwarded-For $remote_addr;
            }
        }
    }

这是我的 Docker-compose 文件:

version: '3.9'
services:
    app1basecontainer:
        image: app1baseimg
        build: ./APP1BASEIMG
        volumes:
            - apps1hared:C:\DEFAULT\APPPortal
        expose:
            - 8080
    app2basecontainer:
        depends_on: 
            - "appbasecontainer"
        image: app2baseimg
        build: ./APP2BASEIMG
        volumes:
            - apps2hared:C:\DEFAULT\APPPortal
        expose:
            - 80


volumes:
    apps1hared:
    apps2shared:

非常感谢

2 个选项:

1- 将容器端口绑定到外部,然后代理到它们:

services:
    app1basecontainer:
        ...
        ports:
            - 8080:8080
    app2basecontainer:
        ...
        ports:
            - 80:80

然后,在你的 Nginx conf 中:

server{
    location /APP1 {
        proxy_pass          http://localhost:8080;
    }
    location /APP2 {
        proxy_pass          http://localhost:80;
    }
}

2- Nginx容器到你的docker-compose,然后apps和nginx在同一个网络,nginx可以看到apps容器。然后你的 nginx conf 更改为:

server{
    location /APP1 {
        proxy_pass          http://CONTAINER_NAME:8080;
    }
    location /APP2 {
        proxy_pass          http://CONTAINER_NAME:80;
    }
}