Is there any way to disable a service in docker-compose.yml

DockerDocker Compose

Docker Problem Overview


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" ?

Docker Solutions


Solution 1 - Docker

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.

Solution 2 - Docker

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"]

Solution 3 - Docker

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 of services: > > Sections with the x- prefix will be parsed, but ignored if not used in the intended way as an extension field.

Solution 4 - Docker

I would scale the service to 0 replicas with:

deploy:
      replicas: 0

Unfortunately as the documentation states this only works with Docker Swarm.

Solution 5 - Docker

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.

Solution 6 - Docker

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/

Solution 7 - Docker

I got it:

docker-compose up $(yq -r '.services | keys | join(" ")' docker-compose.yml | sed 's/service-name//')

Solution 8 - Docker

Primitive but add a # at the beginning of each line of the service.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMandragorView Question on Stackoverflow
Solution 1 - DockermcarsonView Answer on Stackoverflow
Solution 2 - DockerKos ProvView Answer on Stackoverflow
Solution 3 - DockerVonCView Answer on Stackoverflow
Solution 4 - DockerhermView Answer on Stackoverflow
Solution 5 - DockerMichele MinnoView Answer on Stackoverflow
Solution 6 - DockerShibashisView Answer on Stackoverflow
Solution 7 - DockerCodeGuyView Answer on Stackoverflow
Solution 8 - DockerKjeld FlarupView Answer on Stackoverflow