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

Photo of author
Written By M Ibrahim
boot2docker docker-compose

Quick Fix: Replace the service’s command or entrypoint with /bin/true to stop it from doing anything.

The Problem:

In a docker-compose.yml file, there is a need to temporarily disable a service without commenting it out. Is there an option to explicitly set the service’s enabled state to false?

The Solutions:

Solution 1: Redefine `entrypoint` or `command`

You can redefine the `entrypoint` or `command` in your `docker-compose.yml` file to disable a service temporarily. Replace the original command with something that does nothing, such as `/bin/true`. This will cause the container to exit immediately, effectively disabling the service.

Solution 2: Use the `x-disabled:` section (top-level)

Docker Compose provides a top-level section called `x-disabled:` that can be used to disable services. Move the services you want to disable to this section. Sections with the `x-` prefix will be parsed but are ignored if not used as extension fields, so this is a convenient way to disable services without having to comment them out or redefine their `entrypoint`.

Solution 2: Using profiles in docker-compose.yml

Docker Compose 1.28.0 introduced support for a profiles key, allowing you to elegantly disable a service or selectively run certain services.

You can include the following in your docker-compose.yml file to disable a service named base_image:

version: "3.9"
services:
  base_image:
    ...
    profiles:
      - donotstart

This approach enables you to create groups of containers that run together based on a --profile option specified on the command line. Refer to the documentation at https://docs.docker.com/compose/profiles/ for detailed examples.

Update:

Support for profiles now functions correctly in Compose V2 beta 5 (docker compose) and is included in Docker Desktop 3.5.2, released on 2021-07-08. For more information, see Compose V2 beta 5 release notes and Docker Desktop 3.5.2 release notes.

Solution 3: Override the service entrypoint in docker-compose.override.yaml

To disable a service in docker-compose.yml, you can create a docker-compose.override.yaml file. This file is automatically read and merged into the main docker-compose.yaml by Docker Compose. You can exclude this override file from Git, allowing each developer to tweak the configuration without changing the original docker-compose.yaml.

To disable a service named foo, redefine its entrypoint in docker-compose.override.yaml as follows:

“`
version: “3”

services:
foo:
entrypoint: ["echo", "Service foo disabled"]


<p>
When you run <code>docker-compose up</code>, the <code>foo</code> service will be started, but it will immediately print the message "Service foo disabled" and exit. This effectively disables the service.
</p>

Solution 4: Assign service to profile(s)

To disable a service temporarily in a docker-compose file, you can assign the service to a profile and then use the --profile option when running docker-compose up to specify which profiles should be enabled.

For example, to disable the dev_service service, you would add the following to your docker-compose.yml file:

services:
  dev_service:
    ...
    profiles: ["dev"]

Then, when you run docker-compose up, you would use the --profile option to specify that only the dev profile should be enabled:

docker compose --profile dev up

This would prevent the dev_service service from being started.

For more information, see the Docker documentation on profiles:

[https://docs.docker.com/compose/profiles/][1]

Solution 5: Use replicas to disable a service

To disable a service temporarily in a `docker-compose` file, you can scale the service to 0 replicas. This will stop the service without removing it from the `docker-compose` file. You can use the following syntax in your `docker-compose.yml` file:

deploy:
      replicas: 0

However, it’s worth noting that this method only works with Docker Swarm, as mentioned in the [official documentation][1].