Stop and Remove Compose Services
Learn to stop and remove Docker Compose services with practical, step-by-step instructions. This lesson covers core concepts, hands-on exercises, troubleshooting common issues, and preparing for next steps in the Docker track.
Focus: stop and remove compose services
You've built a multi-service application with docker compose up, and everything is running smoothly. But now you need to stop those services — maybe to free up resources, test a new configuration, or deploy an update. Simply killing the terminal or hitting Ctrl+C can leave dangling containers, networks, and volumes that cause conflicts next time. That's where properly stopping and removing Compose services comes in.
The problem this lesson solves
When you run docker compose up, Compose creates containers, networks, and optionally volumes for your services. Shutting down the application isn't as simple as stopping the containers. Leftover resources can:
- Consume disk space — stopped containers, unused networks, and anonymous volumes linger.
- Block port bindings — a stopped container still holds a port if not removed.
- Cause stale data — databases may retain test data from previous runs.
- Confuse other tools — CI/CD pipelines or other projects may misinterpret old containers.
The core pain is that docker stop only halts containers but doesn't clean up. When you need to rebuild cleanly or start fresh, you must remove those services completely. This lesson shows you how to do that efficiently with Docker Compose.
Core concept / mental model
Think of Compose services as a set of Lego bricks (containers, networks, volumes) arranged according to a blueprint (the docker-compose.yml file).
docker compose stop— freezes the bricks in place. The containers are paused without deleting them. You can restart them later withdocker compose start, and all data persists.docker compose down— disassembles the entire structure. It stops all services and removes the containers, networks, and — optionally — volumes and images.
Pro tip: Use
docker compose downwhen you want a clean slate for a freshup. Usedocker compose stopwhen you only need a temporary pause.
Key definitions
| Term | Meaning |
|---|---|
| Stop | Pause container processes; resources (CID, networks) preserved |
| Remove | Delete containers, networks, and optionally volumes/images |
docker compose down |
Stop + remove containers + networks |
docker compose rm |
Remove stopped containers only (must stop first) |
docker compose down -v |
Stop + remove + delete volumes |
docker compose down --rmi all |
Stop + remove + delete all images used by services |
How it works step by step
Follow this logical progression to understand the lifecycle of stopping and removing Compose services.
Step 1: Check current Compose project
Before stopping, see what is currently running:
docker compose ps
This shows all services defined in the Compose file. Each has a status (Up, Paused, Exited).
Step 2: Stop services temporarily
Use docker compose stop to halt all containers without removal:
docker compose stop
Containers are stopped but still exist. Run docker compose ps again to see Exit 0 status. You can restart them with:
docker compose start
Step 3: Remove containers and networks (but keep volumes)
For a clean teardown that preserves persistent data, use:
docker compose down
This removes:
- All containers of the project
- Default network(s) created by Compose
- But not volumes declared in volumes: or bound mounts
Step 4: Remove everything — including volumes
If you want to delete database data, cache, or temp files, add the -v flag:
docker compose down -v
This removes anonymous volumes and named volumes defined in volumes:. Beware — data loss is permanent.
Step 5: Remove images as well (full cleanup)
In CI/CD or when testing image changes, you may want to also delete the pulled or built images:
docker compose down --rmi all
This removes all images used by any service in the Compose file. You can also choose --rmi local to remove only images built locally.
Hands-on walkthrough
Let's practice with a simple web service + database example.
1. Create a sample docker-compose.yml
version: '3.8'
services:
web:
image: nginx:latest
ports:
- "8080:80"
db:
image: postgres:13
environment:
POSTGRES_PASSWORD: example
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:
2. Start the services
docker compose up -d
Expected output:
Creating network "myapp_default" with driver "bridge"
Creating myapp_web_1 ... done
Creating myapp_db_1 ... done
Check running state:
docker compose ps
You'll see both services Up.
3. Stop the services (temporary)
docker compose stop
Expected output:
Stopping myapp_db_1 ... done
Stopping myapp_web_1 ... done
Now docker compose ps shows Exit 0 for both.
4. Start them again to verify persistence
docker compose start
Services return to Up. Data in the Postgres volume is still there.
5. Remove services but keep volumes
docker compose down
Expected output:
Stopping myapp_web_1 ... done
Stopping myapp_db_1 ... done
Removing myapp_web_1 ... done
Removing myapp_db_1 ... done
Removing network myapp_default
Note: the pgdata volume remains. You can verify with:
docker volume ls | grep pgdata
6. Start fresh without data
docker compose down -v
docker compose up -d
The Postgres volume is deleted and recreated with a clean database.
Compare options / when to choose what
| Command | What it does | When to use |
|---|---|---|
docker compose stop |
Stops containers, keeps everything | Temporary pause, restart later |
docker compose down |
Stops + removes containers + networks | Clean slate for next project run |
docker compose down -v |
Same + removes volumes | Full reset (careful: data loss) |
docker compose down --rmi all |
Same + removes images | Fresh rebuild from scratch |
docker compose rm |
Remove stopped containers only | Selective removal after stop |
Variations
- Stop a single service:
docker compose stop webstops only thewebservice. - Remove a single container:
docker compose rm dbremoves the stoppeddbcontainer (must stop first). - Remove without stopping:
docker compose downautomatically stops containers first.
Troubleshooting & edge cases
Error: "no configuration file provided: not found"
You're in the wrong directory or the Compose file is named differently. Use -f:
docker compose -f /path/to/docker-compose.yml down
Error: "service 'web' is not running"
Running docker compose stop on already-stopped services is harmless — it outputs "Stopping" but does nothing. Similarly, docker compose down works fine on stopped services.
Error: "Failed to remove network myapp_default: network is still in use"
Another container outside Compose is attached to the network. Remove that container first, or force remove with docker network rm -f myapp_default (manual).
Problem: Containers not removed after docker compose down
You might have run docker compose up without the config file or with a different project name. Use:
docker compose -p myproject down
where myproject is the actual project name (defaults to directory name).
Edge case: Orphaned containers
If you change the Compose file (e.g., remove a service), old containers won't be removed by down. Use --remove-orphans:
docker compose down --remove-orphans
Common mistakes
- Running
docker compose stopand expecting a clean state — stopped containers still consume space and port mappings. - Using
docker compose down -vwithout backing up database volumes — data is gone forever. - Forgetting to specify
-fwhen working outside the project directory. - Assuming
docker compose rmstops containers (it doesn't; only removes stopped ones). - Confusing
docker compose downwithdocker compose stopwhen you need to restart later.
What you learned & what's next
You now understand how to properly stop and remove Compose services:
- Stopping preserves resources for a quick restart.
- Removing cleans up containers, networks, and optionally volumes/images.
docker compose downis the go-to command for teardown.- Use flags (-v, --rmi) for deeper cleanup.
- Troubleshoot common errors like missing config files or orphans.
This skill is essential for development workflows, CI/CD pipelines, and production maintenance. Your next step is learning how to view logs and debug services, ensuring you can diagnose issues when things don't work as expected.
Pro tip: Create an alias like
dc-downfordocker compose down --remove-orphansto keep your environment clean in daily use.
Practice recap
Create a new directory with a docker-compose.yml defining an Nginx service and a PostgreSQL service. Practice: start the services with docker compose up -d, stop them with docker compose stop, then remove them with docker compose down -v. Verify no containers or volumes remain with docker ps -a and docker volume ls. Experiment with the --rmi all flag and observe the image removal.
Common mistakes
- Running
docker compose stopand expecting a clean slate — stopped containers still hold ports and disk space. - Using
docker compose down -vwithout backing up important database volumes — data loss is permanent. - Forgetting to specify
-fwhen running Compose commands outside the project directory, causing 'no configuration file' errors. - Assuming
docker compose rmalso stops containers — it only removes already stopped ones. - Confusing
docker compose downwithdocker compose stop— usestopfor temporary pause,downfor full cleanup.
Variations
- Use
docker compose stop SERVICE_NAMEto halt a single service while others continue running. - Use
docker compose down --rmi localto remove only locally built images, preserving pulled ones. - Use
docker compose down --timeout 10to override the default 10-second grace period before force-stopping containers.
Real-world use cases
- Resetting a development environment to test a fresh database schema without manual cleanup.
- Tearing down a staging environment after a CI/CD pipeline to free server resources.
- Restarting a multi-service application after updating configuration or environment variables.
Key takeaways
- Use
docker compose stopfor a temporary pause; usedocker compose downfor full cleanup. - Add
-vtodownto delete volumes (irreversible data loss — handle with care). - Add
--rmi allor--rmi localto remove images during teardown. - Use
--remove-orphansto clean up containers from outdated service definitions. - Always specify the correct project name or use
-fwhen running Compose outside the project directory.
Keep learning
Related tutorials, quizzes, and articles for this topic.
Discussion
Questions, corrections, and tips help everyone reading this page.
0 comments
Add a comment
No comments yet — start the thread.