Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Docker commands Guide – docker restart with examples

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

CommandDescription
docker restart my_containerRestart a single container
docker restart stopped_containerRestart a stopped container
docker restart container1 container2Restart multiple containers
docker restart -t 20 my_containerRestart 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 a1b2c3d4e5f6Restart a container using its ID

Best Practices for Using docker restart:

  1. Use custom timeouts (-t) for services that need extra time to shut down gracefully.
  2. Monitor container health after restarting to ensure services resume correctly.
  3. Combine with docker logs to check for errors after restarting.
  4. Avoid frequent restarts for critical services—consider using health checks and restart policies.
  5. Automate container restarts using cron jobs or scripts for scheduled maintenance.

Common Errors and Solutions

  1. “No such container”
    → Ensure the container name or ID is correct. Use docker ps -a to find it.
  2. “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
  3. 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.

Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x