How to Use docker-compose up for Only Certain Containers
TLDR
You don't have to start every service in your docker-compose.yml
at once. Use docker-compose up <service1> <service2>
to launch only the containers you need. This is handy for development, testing, or when you want to save resources.
Why Start Only Certain Containers?
In real-world projects, your docker-compose.yml
might define several services—databases, caches, web apps, workers, and more. Sometimes you only want to run a subset, like just the API and database for local development, or a single worker for debugging.
How to Start Specific Services
The syntax is simple:
docker-compose up <service1> <service2>
Replace <service1>
and <service2>
with the names of the services as defined in your docker-compose.yml
.
Example:
Suppose your docker-compose.yml
looks like this:
version: '3.8'
services:
db:
image: postgres:15
redis:
image: redis:7
web:
build: ./web
depends_on:
- db
- redis
worker:
build: ./worker
depends_on:
- db
To start only the web
and db
services:
docker-compose up web db
This will:
- Start
db
first - Build and start
web
- Not start
redis
orworker
If a service depends on another, Docker Compose will start the dependencies automatically if needed.
Stopping and Removing Only Certain Containers
You can also stop or remove specific services:
docker-compose stop web db
docker-compose rm web db
This stops or removes just those containers, leaving others running.
Tips for Multi-Service Projects
- You can list as many services as you want:
docker-compose up service1 service2 ...
- If you run
docker-compose up
with no arguments, all services are started. - Use
-d
to run in detached mode:docker-compose up -d web db
- To see logs for just certain services:
docker-compose logs web db
Conclusion
Starting only the containers you need with docker-compose up
is a great way to speed up development and save resources. Just specify the service names, and Docker Compose will handle dependencies for you. Check your service names in the YAML file, and use this trick to streamline your workflow.
Found an issue?