你能定义可选的 docker-compose 服务吗? [英] Can you define optional docker-compose services?
问题描述
是否有一种方法可以定义 Docker Compose 服务,使其仅在您明确请求时启动
?
Is there a way to define a Docker Compose service such that it is only brought up
when you explicitly request it?
也就是说:
docker-compose up
不会启动它,但是
docker-compose up optional_service
会.
推荐答案
Docker compose 现在支持配置文件,它允许你 a) 默认禁用一个或多个服务,b) 在 docker compose 运行时启用它们--profile
参数.
Docker compose now supports profiles, which allow you to a) disable one or more services by default, and b) enable them when docker compose is ran with the corresponding --profile
argument.
这是通过在 docker-compose.yml
文件中向您的服务添加一个 profiles
键来完成的.没有这个密钥的服务总是由 docker-compose 启动(对于所有配置文件).
This is done by adding a profiles
key to your service in the docker-compose.yml
file. Services that have don't have this key are always started by docker-compose (for all profiles).
例如,使用以下 docker-compose.yml 文件
For example, with the following docker-compose.yml file
version: '3.9'
services
app_service:
# ...
test_service:
profiles:
- testing
# ...
运行 docker-compose up
只会启动 app_service
,运行 docker-compose --profile testing up
会启动 app_service
code>app_service 和 test_service
.
Running docker-compose up
will only start the app_service
, and running docker-compose --profile testing up
will start both the app_service
and the test_service
.
可以通过传递多个 --profile
标志来指定多个配置文件.
Multiple profiles can be specified by passing multiple --profile
flags.
更多信息可以在这里找到:https://docs.docker.com/compose/profiles/
More info can be found here: https://docs.docker.com/compose/profiles/
我必须更新到 docker 引擎 v20.10.5 才能工作.
I had to update to docker engine v20.10.5 for this to work.
这篇关于你能定义可选的 docker-compose 服务吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!