这将使用 3 个独立的数据库(以 nginx 和 mongo 为例)启动服务集。
version: '3'
services:
web1:
container_name: web1
image: nginx:latest
ports:
- 8080:8080
app1:
container_name: app1
image: nginx:latest
ports:
- "8081:8081"
db1:
container_name: db1
image: mongo
ports:
- 27017
web2:
container_name: web2
image: nginx:latest
ports:
- 8082:8082
app2:
container_name: app2
image: nginx:latest
ports:
- "8083:8083"
db2:
container_name: db2
image: mongo
ports:
- 27018
web3:
container_name: web3
image: nginx:latest
ports:
- 8084:8084
app3:
container_name: app3
image: nginx:latest
ports:
- "8085:8085"
db3:
container_name: db3
image: mongo
ports:
- 27019
本地测试:
NAMES STATUS PORTS IMAGE
db2 Up About a minute 27017/tcp, 0.0.0.0:32803->27018/tcp mongo
web1 Up About a minute 80/tcp, 0.0.0.0:32802->8080/tcp nginx:latest
db1 Up About a minute 0.0.0.0:32801->27017/tcp mongo
app1 Up About a minute 80/tcp, 0.0.0.0:32800->8081/tcp nginx:latest
app3 Up About a minute 80/tcp, 0.0.0.0:32798->8085/tcp nginx:latest
db3 Up About a minute 27017/tcp, 0.0.0.0:32799->27019/tcp mongo
app2 Up About a minute 80/tcp, 0.0.0.0:32797->8083/tcp nginx:latest
web3 Up About a minute 80/tcp, 0.0.0.0:32796->8084/tcp nginx:latest
web2 Up About a minute 80/tcp, 0.0.0.0:32795->8082/tcp nginx:latest