Is there any way to disable a service in docker-co

2019-03-14 08:36发布

I find myself in the situation, that I want to disable a service temporarily in a docker-compose file.

Of course I could comment it out, but is there any option to just say "enabled: false" ?

5条回答
Fickle 薄情
2楼-- · 2019-03-14 08:51

There is no way to disable a service defined in Docker compose yaml file. VonC's suggestion is a good workaround Please see below the docker compose documentation for available options https://docs.docker.com/compose/compose-file/

查看更多
做个烂人
3楼-- · 2019-03-14 08:52

I would scale the service to 0 replicas with: deploy: replicas: 0

Unfortunately as the documentation states this only works with docker swarm.

查看更多
\"骚年 ilove
4楼-- · 2019-03-14 08:53

I add the following extra line to the service I want to temporarily disable:

command: echo "{put your service name here} disabled"

It starts anyway, but does nothing.

查看更多
贪生不怕死
5楼-- · 2019-03-14 08:54

You can do it in a docker-compose.override.yaml file.

This file is automatically read by docker-compose and merged into the main docker-compose.yaml.

If you have it excluded from Git, each developer can tweak the configuration (with a few limitations) without changing the original docker-compose.yaml.

So, service foo can be disabled ad-hoc by redefining its entrypoint in docker-compose.override.yaml:

version: "3"

services:
  foo:
    entrypoint: ["echo", "Service foo disabled"]
查看更多
不美不萌又怎样
6楼-- · 2019-03-14 09:02

You could simply redefine the entrypoint or command in order to replace said command with something which does nothing (/bin/true)

That would make the container exit immediately, doing nothing.


shadi adds the following tips in the comments:

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"]
查看更多
登录 后发表回答