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
" ?
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.
COMPOSE_PROFILES=production
.
COMPOSE_PROFILES
environment variable. I probably confused things by referring to this earlier as a "default profile."
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"]
export XXX_ENTRYPOINT=/bin/true
then ` entrypoint: [ "${XXX_ENTRYPOINT:-docker-entrypoint.sh}"]`
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.
restart: "no"
to avoid infinite restarts
x-disabled:
instead of services:
x-
section is github.com/compose-spec/compose-spec/blob/master/…, but it is possible indeed that x-disabled
is no longer valid.
services:
dev_service:
...
profiles: ["dev"] # only runs with dev profile
docker compose --profile dev up
I would scale the service to 0 replicas with:
deploy:
replicas: 0
Unfortunately as the documentation states this only works with Docker Swarm.
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.
command
does not have an effect on the entry point. In your example you rely that the entrypoint
is bash
I guess. To make this resilient ((independent of the built-in entrypoint
) I think you need to redefine the entry point, not the command.
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/
I finally found a solution to mimic conditionally run ALL by default except XXX
, while avoiding manipulating profiles and breaking basic usage.
Let's say you have your own docker-compose.yaml
, create an override docker-compose.override.yaml
that will be automatically taken into account like:
services:
prestashop:
profiles:
- ${DISABLE_PRESTASHOP:-}
postgres:
profiles:
- ${DISABLE_APP:-}
mariadb:
profiles:
- ${DISABLE_PRESTASHOP:-}
mailcatcher:
profiles:
- ${DISABLE_PRESTASHOP:-}
DISABLE_PRESTASHOP=true docker-compose up
only the postgres
service will startDISABLE_APP=true docker-compose up
, prestashop / mariadb / mailcatcher
will rundocker-compose up
, since Docker does not assign an empty string value, it will run all servicesNotes:
DISABLE_APP=whatever
it would act as DISABLE_APP=true
since it's the profile name we set as required to be launcheddocker-compose down
will shutdown all launched services due to no profile setFrom Docker Compose version 2.24.4 and later you can use !reset
and !override
YAML tags like this:
docker-compose.yml
:
services:
db:
...
service2:
...
service3:
depends_on:
- db
- service2
docker-compose.override.yml
:
services:
db: !reset
service3:
depends_on: !override
- service2
https://docs.docker.com/compose/compose-file/13-merge/#reset-value
I got it:
docker-compose up $(yq -r '.services | keys | join(" ")' docker-compose.yml | sed 's/service-name//')
yq eval '.services | keys | .[] | select(. != "backup")' docker-compose.yml
Commented
Dec 5, 2021 at 9:09
docker-compose up
it will start all the services by default. However, if you rundocker-compose up myservice
it will start myservice and things that depend on it. By setting up the dependencies you can make it so the bad service doesn't start with this command. You can also dodocker-compose run
to get just the services you want. The right choice may also be to break this into multiple compose files to allow you the flexibility you need.--no-deps
in case you don't want to start the dependencies. I know is not what you are looking for, but is the other way around.docker-compose -f docker-compose.yml -f another-docker-compose.yml up -d
. You can check the resultant docker compose merge with the config command:docker-compose -f docker-compose.yml -f another-docker-compose.yml config