The docker restart command is a powerful tool for managing Docker containers, allowing you to restart one or more running or stopped containers. This guide will provide a detailed overview of the docker restart command, including various options and examples to help you manage your Docker containers effectively.

What is the docker restart Command?

The docker restart command stops and then starts one or more containers. It can be used to restart containers that are either running or stopped, making it useful for applying configuration changes or recovering from errors.

Basic Syntax

The basic syntax for the docker restart command is:

docker restart [OPTIONS] CONTAINER [CONTAINER...]

Restarting a Container

To restart a running or stopped container, use the following command:

docker restart <container_id>

Replace <container_id> with the ID or name of the container you want to restart. For example:

docker restart my_nginx

Restarting Multiple Containers

To restart multiple containers at once, list the container IDs or names separated by spaces:

docker restart container1 container2 container3

Specifying a Timeout

By default, Docker waits 10 seconds before forcibly stopping a container. You can specify a different timeout in seconds using the -t option:

docker restart -t 5 my_nginx

This command sets a 5-second timeout before stopping and restarting the container.

Conclusion

The docker restart command is a vital tool for managing the lifecycle of your Docker containers. By using this command, you can easily restart containers, making it a valuable tool for maintaining your containerized applications. Whether you need to restart a single container or multiple containers, the docker restart command provides the flexibility and control you need.