如果我在 docker-compose 项目中定义了很多服务,如何从默认的 docker-compose up
命令中排除某个服务?
例如,我有一个 nginx 服务和一个 ssl 服务。它们发生冲突,因为它们都消耗端口 80,所以我怎样才能使 ssl 服务不会启动,除非我专门运行该服务,并且默认情况下 up
命令将执行除 ssl 之外的所有操作服务?
最佳答案
以 docker-compose
1.28.0 开头新service profiles就是为此而生的!使用profiles
,您可以将服务标记为仅在特定配置文件中启动,例如:
services:
webapp:
# ...
nginx:
# ...
profiles: ["nginx"]
ports:
- 80:80
ssl:
# ...
profiles: ["ssl"]
ports:
- 80:80
docker-compose up # start only your webapp services
docker-compose --profile nginx up # start the webapp and nginx service
docker-compose --profile ssl up # start the webapp and ssl service
docker-compose run ssl # run the ssl service
根据您的具体用例/设置,将您的服务拆分为 multiple docker-compose.yml
files 可能会更好.
关于Docker-compose 默认排除服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50848797/