使用docker compose和nginx进行多个网络路由



我正在寻找一种可以在单个 AWS EC2 主机上托管多个网站的环境。所以,我有一个应用程序,其中我有多个组件,每个组件作为 docker 的一部分,它们是 docker 组合的一部分。每个 docker 公开一个端口,比如组件公开 9443。

因此,我想要的是在单个EC2主机上部署我的应用程序的多个副本,并使用nginx路由到它们。 因此,组件stack1_componenta、stack1_componentb、stack2_componenta stack2_component2可能有多个副本。因此,stack1_componenta公开的端口 9443 应仅位于堆栈 1 内部,而不应由堆栈 2 访问。

有没有办法为每个码头工人组成定义一个网络,并根据域将nginx路由到该网络?

感谢大家的评论和解决方案。我能够使用EXPOSE,network_type=bridge和nginx来实现这一点。因此,使用 EXPOSE 而不是 PORTS,我将仅在撰写环境中提供这些端口,并且我已经使用 nginx 打开了组合外部的端口。 示例撰写文件以供参考:

version: '3'
services:
component1:
build: ./component1
image: <repo-url>/component1:${VER}
expose:
- <portnumber>
volumes:
- "<host-path>:<docker-path>"
network_mode: bridge
command: bash -c "while true; do sleep 1; done"
nginx:
build: ./nginx
image: <repo-url>/nginx:${VER}
depends_on:
- "component1"
links:
- "component1:component1"
ports:
- 1443:443
volumes:
- ${BASEFOLDERPATH}/nginx/log/:/var/log/nginx/
restart: on-failure
network_mode: bridge
command: bash -c "nginx; while true; do sleep 1; done"

最新更新