Kritim Yantra
Apr 25, 2025
When you work with Docker Compose in real-world applications, you often deal with multiple services: backend, frontend, database, cache, etc.
But what if you don’t always need to run all the services? What if you want to test only the backend or rebuild just the database?
That’s where Named Subsets of Services come in handy.
In this blog, you’ll learn:
Let’s dive in!
A named subset of services allows you to define a group (subset) of services in your Docker Compose project and run only those services when needed — without spinning up the entire stack.
This is super useful in large, modular apps where you might want to:
You don’t have to wait for all services to build/start if you're only working on one.
Reduces CPU/memory usage during development or testing.
Narrow down issues without unrelated containers running in the background.
You can define subsets manually by passing service names in your docker-compose
commands.
docker-compose.yml
version: "3.9"
services:
backend:
build: ./backend
ports:
- "8000:8000"
frontend:
build: ./frontend
ports:
- "3000:3000"
db:
image: postgres:14
environment:
POSTGRES_PASSWORD: example
redis:
image: redis:alpine
Now you have 4 services: backend
, frontend
, db
, and redis
.
Let’s say you only want to start the backend and database.
docker-compose up backend db
You can also use --build
if you want to rebuild just these two:
docker-compose up --build backend db
Or just build them:
docker-compose build backend db
Want to test only the frontend?
docker-compose up frontend
Need to start Redis alone for testing cache?
docker-compose up redis
To manage subsets better, create override files like:
docker-compose -f docker-compose.yml -f docker-compose.dev.yml up backend db
This way, you can define different sets of services in different files (e.g., dev, test, prod) and reuse them easily.
docker-compose.dev.yml
services:
backend:
environment:
NODE_ENV: development
frontend:
environment:
NODE_ENV: development
Now you can run:
docker-compose -f docker-compose.yml -f docker-compose.dev.yml up backend
This gives you dev-specific configs without changing your main docker-compose.yml
.
api
, web
, db
, cache
) so they’re easy to reference.--build
with subsets when testing build changes.profiles
(if using Compose v3.9+) for advanced setups.profiles
for Advanced ControlCompose v3.9+ supports profiles
, which are a more advanced way to define subsets.
services:
backend:
build: ./backend
profiles: ["api"]
db:
image: postgres
profiles: ["api", "db"]
redis:
image: redis
profiles: ["cache"]
Now run:
docker-compose --profile api up
Only the services in the api
profile will start!
Feature | Benefit |
---|---|
Named subsets | Run only the services you need |
Faster feedback | Save time by avoiding full builds |
Better control | Easier to test and debug individual services |
Flexible setup | Combine with override files or profiles |
Named subsets of services are a simple but powerful trick in Docker Compose that helps you work smarter, not harder. Whether you're working on a monorepo or a microservice-based app, being able to isolate services can save you time, resources, and headaches.
Start using them today — and make your dev workflow feel lighter.
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google