有什么方法可以禁用 docker-compose.yml 中的服务
Is there any way to disable a service in docker-compose.yml
我发现自己处于这样一种情况,我想在 docker-compose
文件中临时禁用一项服务。
当然我可以把它注释掉,但是有什么选项可以只说“enabled: false
”吗?
您可以简单地重新定义 entrypoint
or command
以便用不执行任何操作的命令替换所述命令 (/bin/true
)
这将使容器立即退出,什么也不做。
shadi在评论中添加以下提示:
If you don't want the service to get built at all, redefine the build key to point to a Dockerfile
that only has:
FROM tianon/true
ENTRYPOINT ["/true"]
5andr0 points out in the comments the top-level section x-disabled:
(类似扩展字段)
Far more convenient: moving disabled services to the top-level section x-disabled:
instead of services:
Sections with the x-
prefix will be parsed, but ignored if not used in the intended way as an extension field.
无法禁用 Docker compose yaml 文件中定义的服务。
VonC 的建议是一个很好的解决方法
请参阅下面的 docker 撰写文档以了解可用选项
https://docs.docker.com/compose/compose-file/
我将以下额外行添加到我想暂时禁用的服务中:
command: echo "{put your service name here} disabled"
它无论如何都会启动,但什么也不做。
我会将服务扩展到 0 个副本:
deploy:
replicas: 0
不幸的是,作为 documentation states,此 仅适用于 Docker Swarm。
您可以在 docker-compose.override.yaml
文件中完成。
这个文件被docker-compose
自动读取并合并到主docker-compose.yaml
中。
如果您将它从 Git 中排除,每个开发人员都可以在不更改原始 docker-compose.yaml
.
的情况下调整配置(有一些限制)
因此,可以通过在 docker-compose.override.yaml
中重新定义其入口点来临时禁用服务 foo
:
version: "3"
services:
foo:
entrypoint: ["echo", "Service foo disabled"]
原始但在每行服务的开头添加一个#
。
我知道了:
docker-compose up $(yq -r '.services | keys | join(" ")' docker-compose.yml | sed 's/service-name//')
截至 2021 年 1 月,有一种方法可以优雅地禁用 docker-compose.yml
中的服务,或者有选择地 运行 某些服务而不是其他服务。 Docker Compose 1.28.0 引入了对 profiles
键的支持。现在我们可以这样做:
version: "3.9"
services:
base_image:
...
profiles:
- donotstart
文档中的示例描述了如何使用此密钥创建容器组,这些容器 运行 基于命令行上的 --profile
选项组合在一起。在此处查看页面:https://docs.docker.com/compose/profiles/
更新
在 2021 年 7 月 8 日发布的 Compose V2 beta 5 (docker compose
). Compose V2 beta 6 has been included in Docker Desktop 3.5.2 中,对配置文件的支持正常工作。
我发现自己处于这样一种情况,我想在 docker-compose
文件中临时禁用一项服务。
当然我可以把它注释掉,但是有什么选项可以只说“enabled: false
”吗?
您可以简单地重新定义 entrypoint
or command
以便用不执行任何操作的命令替换所述命令 (/bin/true
)
这将使容器立即退出,什么也不做。
shadi在评论中添加以下提示:
If you don't want the service to get built at all, redefine the build key to point to a
Dockerfile
that only has:
FROM tianon/true
ENTRYPOINT ["/true"]
5andr0 points out in the comments the top-level section x-disabled:
(类似扩展字段)
Far more convenient: moving disabled services to the top-level section
x-disabled:
instead ofservices:
Sections with the
x-
prefix will be parsed, but ignored if not used in the intended way as an extension field.
无法禁用 Docker compose yaml 文件中定义的服务。 VonC 的建议是一个很好的解决方法 请参阅下面的 docker 撰写文档以了解可用选项 https://docs.docker.com/compose/compose-file/
我将以下额外行添加到我想暂时禁用的服务中:
command: echo "{put your service name here} disabled"
它无论如何都会启动,但什么也不做。
我会将服务扩展到 0 个副本:
deploy:
replicas: 0
不幸的是,作为 documentation states,此 仅适用于 Docker Swarm。
您可以在 docker-compose.override.yaml
文件中完成。
这个文件被docker-compose
自动读取并合并到主docker-compose.yaml
中。
如果您将它从 Git 中排除,每个开发人员都可以在不更改原始 docker-compose.yaml
.
因此,可以通过在 docker-compose.override.yaml
中重新定义其入口点来临时禁用服务 foo
:
version: "3"
services:
foo:
entrypoint: ["echo", "Service foo disabled"]
原始但在每行服务的开头添加一个#
。
我知道了:
docker-compose up $(yq -r '.services | keys | join(" ")' docker-compose.yml | sed 's/service-name//')
截至 2021 年 1 月,有一种方法可以优雅地禁用 docker-compose.yml
中的服务,或者有选择地 运行 某些服务而不是其他服务。 Docker Compose 1.28.0 引入了对 profiles
键的支持。现在我们可以这样做:
version: "3.9"
services:
base_image:
...
profiles:
- donotstart
文档中的示例描述了如何使用此密钥创建容器组,这些容器 运行 基于命令行上的 --profile
选项组合在一起。在此处查看页面:https://docs.docker.com/compose/profiles/
更新
在 2021 年 7 月 8 日发布的 Compose V2 beta 5 (docker compose
). Compose V2 beta 6 has been included in Docker Desktop 3.5.2 中,对配置文件的支持正常工作。