Here’s a complete tutorial on docker stop
, including its purpose, how to use it, examples, and use cases.
What is docker stop
?
docker stop
is a Docker command used to gracefully stop a running container by sending the SIGTERM
signal. This allows the main process inside the container to perform necessary cleanup before shutting down. If the container doesn’t stop within the specified timeout (default is 10 seconds), Docker sends a SIGKILL
signal to forcefully stop it.
Key Features:
- Stops running containers gracefully (tries to avoid data loss).
- Allows you to specify a timeout before forcing the stop.
- Can stop multiple containers at once.
- Useful for service management, maintenance, and resource control.
Basic Syntax
docker stop [OPTIONS] CONTAINER [CONTAINER...]
Common Options:
-t
,--time
: Specify the timeout (in seconds) before forcefully stopping the container. The default is 10 seconds.
Examples of docker stop
1. Stop a Running Container
docker stop my_container
This sends a SIGTERM
to the container named my_container
and waits up to 10 seconds before forcefully stopping it.
2. Stop a Container by ID
docker stop a1b2c3d4e5f6
You can stop a container using its unique container ID.
3. Stop Multiple Containers
docker stop container1 container2 container3
This command stops multiple containers simultaneously.
4. Stop All Running Containers
docker stop $(docker ps -q)
This stops all currently running containers.
5. Specify a Custom Timeout
docker stop -t 20 my_container
This gives the container 20 seconds to stop gracefully before sending SIGKILL
.
6. Use in a Shell Script
#!/bin/bash
docker stop web_container db_container cache_container
echo "Containers stopped successfully."
This script stops specific containers and prints a success message.
7. Stop the Last Created Container
docker stop $(docker ps -lq)
This stops the most recently created container.
8. Stop Containers with Specific Filters
Stop all containers that were created from a specific image:
docker ps -q --filter "ancestor=nginx" | xargs docker stop
9. Monitor and Stop Non-Responsive Containers
docker inspect --format='{{.State.Status}}' my_container
docker stop my_container
Check the container status before stopping it.
10. Automate Maintenance with docker stop
#!/bin/bash
for container in $(docker ps -q); do
docker stop $container
done
This script stops all running containers one by one.
Use Cases for docker stop
1. Graceful Service Shutdown
- Safely stop services to apply updates or perform maintenance.
- Ensure that services have time to clean up resources or write data before shutting down.
2. Resource Management
- Free up system resources by stopping non-critical containers during high-demand periods.
3. Debugging and Troubleshooting
- Stop unresponsive or malfunctioning containers for investigation.
- Combine with
docker logs
anddocker inspect
to identify issues.
4. Scheduled Maintenance
- Use
docker stop
in scheduled scripts to stop services during off-peak hours.
5. CI/CD Pipelines
- Stop containers after automated tests to ensure a clean environment for the next test cycle.
6. Cleanup and Recovery
- Stop and restart containers during service recovery or when replacing outdated services.
List of Common docker stop
Commands
Command | Description |
---|---|
docker stop my_container | Gracefully stop a running container |
docker stop container1 container2 | Stop multiple containers |
docker stop a1b2c3d4e5f6 | Stop a container using its ID |
docker stop $(docker ps -q) | Stop all running containers |
docker stop -t 20 my_container | Stop a container with a 20-second timeout |
docker stop $(docker ps -lq) | Stop the last created container |
`docker ps -q –filter “ancestor=nginx” | xargs docker stop` |
Best Practices for Using docker stop
:
- Use a reasonable timeout (
-t
) to allow services to shut down cleanly. - Monitor container logs after stopping to verify that the service shut down properly.
- Combine with
docker ps
and filters to target specific containers for stopping. - Avoid using
-t 0
unless necessary—give services at least a few seconds to clean up. - Automate container shutdowns for scheduled maintenance or batch processing.
Common Errors and Solutions
- “No such container”
→ Ensure the container exists and is running. Check withdocker ps
. - “Container failed to stop within the timeout”
→ Increase the timeout (-t
) or usedocker kill
to forcefully stop the container. - “Permission denied”
→ Ensure you have the necessary permissions or run withsudo
. - Service Does Not Restart Properly
→ Check container logs and configuration before restarting.
Combining docker stop
with Other Commands
Stop and Remove a Container
docker stop my_container && docker rm my_container
Stop, Inspect, and Restart
docker stop my_app
docker inspect my_app
docker start my_app
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND