【问题标题】:Multiple networks routing using docker compose and nginx使用 docker compose 和 nginx 进行多网络路由
【发布时间】:2020-06-09 13:42:28
【问题描述】:
我正在寻找可以在单个 AWS EC2 主机上托管多个网站的环境。所以,我有一个应用程序,其中我有多个组件,每个组件都作为 docker,它们是 docker compose 的一部分。每个 docker 都暴露一个端口,比如 componenta 暴露 9443。
所以,我想要的是在单个 EC2 主机上部署我的应用程序的多个副本,并使用 nginx 路由到它们。因此,组件 stack1_componenta、stack1_componentb、stack2_componenta、stack2_component2 可能有多个副本。所以,stack1_componenta 暴露的端口 9443 应该只在 stack1 内部,不应该被 stack2 访问。
有没有一种方法可以为每个 docker compose 定义一个网络,并根据域将 nginx 路由到该网络?
【问题讨论】:
-
-
Networking in Compose 描述了使用 Compose 开箱即用的网络设置;每个 Compose 项目将获得一个单独的 Docker 网络stack1_default、stack2_default、等。 然后您需要编写一个 Nginx 设置,对这些 Compose 文件的发布端口进行基于主机的路由。您遇到了具体问题吗?
标签:
docker
nginx
networking
docker-compose
【解决方案1】:
感谢大家提供的 cmets 和解决方案。我能够使用 EXPOSE、network_type=bridge 和 nginx 来实现这一点。因此,使用 EXPOSE 而不是 PORTS,我将仅在 compose 环境中提供这些端口,并且我已经使用 nginx 在 compose 外部打开了这些端口。
示例撰写文件供参考:
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"