Is there any way to disable a service in docker-compose.yml
As of January 2021, there is a way to elegantly disable a service within the docker-compose.yml
or to selectively run some services and not others. Docker Compose 1.28.0 introduced support for a profiles
key. Now we can do something like:
version: "3.9"
services:
base_image:
...
profiles:
- donotstart
Examples in the documentation describe how to use this key to create groups of containers that run together based on a --profile
option on the command line. Check out the page here: https://docs.docker.com/compose/profiles/
Update
Support for profiles is working correctly in Compose V2 beta 5 (docker compose
). Compose V2 beta 6 has been included in Docker Desktop 3.5.2 released 2021-07-08.
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"]
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"]
5andr0 points out in the comments the top-level section x-disabled:
(an extension field-like)
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.
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.
I would scale the service to 0 replicas with:
deploy:
replicas: 0
Unfortunately as the documentation states this only works with Docker Swarm.