Here’s a complete tutorial on docker restart
, covering how it works, a comprehensive list of examples, and practical use cases.
What is docker restart
?
docker restart
is a Docker command used to stop and then start a container with a single command. It’s useful when you want to refresh or troubleshoot a container without deleting it.
Key Features:
- Stops the container and immediately starts it again.
- Maintains the container’s state and data (not affected by the restart).
- Helps in debugging, configuration changes, and service recovery.
Basic Syntax
docker restart [OPTIONS] CONTAINER [CONTAINER...]
Common Options:
-t
,--time
: Specify the timeout (in seconds) before forcibly stopping the container. The default timeout is 10 seconds.
Examples of docker restart
1. Restart a Running Container
docker restart my_container
This stops and restarts my_container
.
2. Restart a Stopped Container
docker restart stopped_container
This starts a previously stopped container.
3. Restart Multiple Containers
docker restart container1 container2 container3
This command restarts multiple containers simultaneously.
4. Restart a Container with a Custom Timeout
docker restart -t 20 my_container
This gives the container 20 seconds to stop gracefully before being forcibly stopped and restarted.
5. Restart All Running Containers
docker restart $(docker ps -q)
This restarts every running container.
6. Use in a Shell Script
#!/bin/bash
docker restart web_container db_container cache_container
echo "Containers restarted successfully."
This script restarts multiple containers and displays a success message.
7. Restart a Container by ID
docker restart a1b2c3d4e5f6
You can use the container’s ID instead of its name.
8. Restart the Last Created Container
docker restart $(docker ps -lq)
This restarts the most recently created container.
9. Restart Containers with Specific Filters
If you want to restart containers based on their status or name, combine docker ps
with docker restart
:
docker restart $(docker ps -q --filter "status=exited")
This restarts all exited containers.
10. Automate Restarts for a Monitoring System
while true; do
docker restart monitoring_container
sleep 300 # Restart the container every 5 minutes
done
This continuously restarts a monitoring container every 5 minutes.
Use Cases for docker restart
1. Refresh Services
- Restart services to apply configuration changes.
- Useful after environment variable updates or software upgrades.
2. Debugging and Recovery
- Restart non-responsive containers to recover from errors.
- Helps resolve temporary network or resource-related issues.
3. Automated Maintenance
- Schedule periodic container restarts for memory cleanup or log rotation.
- Useful in stateless services where frequent restarts won’t disrupt functionality.
4. CI/CD Pipelines
- Restart containers during continuous integration and deployment to apply changes.
5. Service Dependency Management
- Restart dependent services in a specific order (e.g., restart the database before restarting the web server).
6. Development and Testing
- Quickly restart containers during development to test changes.
- Common in local development environments using Docker Compose.
List of Common docker restart
Commands
Command | Description |
---|---|
docker restart my_container | Restart a single container |
docker restart stopped_container | Restart a stopped container |
docker restart container1 container2 | Restart multiple containers |
docker restart -t 20 my_container | Restart a container with a 20-second timeout |
docker restart $(docker ps -q) | Restart all running containers |
docker restart $(docker ps -lq) | Restart the last created container |
docker restart a1b2c3d4e5f6 | Restart a container using its ID |
Best Practices for Using docker restart
:
- Use custom timeouts (
-t
) for services that need extra time to shut down gracefully. - Monitor container health after restarting to ensure services resume correctly.
- Combine with
docker logs
to check for errors after restarting. - Avoid frequent restarts for critical services—consider using health checks and restart policies.
- Automate container restarts using cron jobs or scripts for scheduled maintenance.
Common Errors and Solutions
- “No such container”
→ Ensure the container name or ID is correct. Usedocker ps -a
to find it. - “Container is already restarting”
→ Check if the container has a restart policy (docker inspect
). Wait for it to finish restarting or stop it manually:docker stop my_container docker restart my_container
- Timeout Issues
→ Increase the timeout with-t
for services that take longer to shut down.
Exit Codes and Troubleshooting
- 0 → Successful restart.
- 1 → Error during restart. Check logs with
docker logs
.
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