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:
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:
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:
Setting DB_REPLICAS to 0 disables the service:
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):
Override file (docker-compose.override.yml):
Usage:
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
| Method | Advantages | Disadvantages |
| Commenting Out | Simple and clear | Manual and error-prone |
| Profiles (v3.9+) | Granular control | Not backward compatible |
| Environment Variables | Automation friendly | Less intuitive |
| Conditional Builds with Overrides | Environment control | Complexity with files |
Related reading
- Issue to node-sass and Docker
- java 8 Docker Improperly specified VM option 'InitialRAMPercentageXX
- Java using much more memory than heap size or size correctly Docker memory limit
- Jenkins docker container always adds cat command
- Is there any way to exec into an initContainer in Kubernetes
- Is there any way to see the file system on the iOS simulator?
- Jenkins running docker commands on a docker slave
- JVM initial CPU spike in a Docker container
System design interview problems on this topic
- Design a Container Orchestration System: solution and walkthrough
- Design a Code Deployment System: solution and walkthrough
- Design GitHub Actions: solution and walkthrough
- Design a Serverless Architecture Framework: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.