docker
docker-compose
services
configuration
troubleshooting

How to disable a service in docker-compose.yml

Codemia Knowledge Hub ·

Docker Compose has no disable or enable option in docker-compose.yml. To keep a service from running, comment it out, assign it a profile, scale it to zero replicas with an environment variable, or override it in a second Compose file.

Commenting out the service

YAML uses # for comments:

yaml
1version: "3"
2services:
3  web:
4    image: nginx
5  # db:
6  #   image: postgres

This is simple and easy to reverse, but it means editing the file by hand every time, which is error-prone.

Putting the service behind a profile

Profiles (Compose file v3.9+) group services so you can start them selectively. A service with profiles only starts when one of its profiles is active, so a plain up leaves db out and passing --profile db_disabled includes it:

yaml
1version: "3.9"
2services:
3  web:
4    image: nginx
5
6  db:
7    image: postgres
8    profiles:
9      - db_disabled
bash
docker compose --profile db_disabled down
docker compose --profile db_disabled up

Profiles give granular control without editing the YAML file, but they require understanding the profile system and are not supported below v3.9.

Scaling the service to zero with an environment variable

Drive the replica count from a variable:

yaml
1version: "3"
2services:
3  web:
4    image: nginx
5
6  db:
7    image: postgres
8    deploy:
9      replicas: ${DB_REPLICAS:-1}

Setting DB_REPLICAS to 0 disables the service:

bash
DB_REPLICAS=0 docker-compose up

This works without editing the file and fits into CI/CD pipelines. It is a lesser-known approach and may not suit every case, especially if you do not otherwise want scaling settings.

Overriding the service in a second Compose file

Compose merges multiple files, so an override file can neutralize a service from the base file.

Base file (docker-compose.yml):

yaml
1version: "3"
2services:
3  web:
4    image: nginx
5  db:
6    image: postgres

Override file (docker-compose.override.yml):

yaml
1version: "3"
2services:
3  db:
4    profiles:
5      - disabled

Usage:

bash
docker-compose up

Compose merges the override file by default, putting db behind a profile that is never activated, so a plain up skips it. This suits separate development and production setups and works well with CI/CD, at the cost of extra files to maintain and more complexity in larger projects.

Comparing the options

MethodAdvantagesDisadvantages
Commenting OutSimple and clearManual and error-prone
Profiles (v3.9+)Granular controlNot backward compatible
Environment VariablesAutomation friendlyLess intuitive
Conditional Builds with OverridesEnvironment controlComplexity with files

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design