The short answer
To restart one or more Docker containers, you can use the docker restart command as follows:
$ docker restart <container …>
Run in Warp
Where:
- container are the names or IDs of the containers that you want to restart, each separated by a space character.
For example:
$ docker restart container_one 90b8831a4b8
Run in Warp
Upon execution, the above command will restart two containers, one with the name container_one and the other identified by its container ID 90b8831a4b8.
Restarting all containers at once
To restart all containers at once, you can use the docker restart command as follows:
$ docker restart $(docker ps -a -q)
Run in Warp
Where:
- $(...) is used for command substitution in Bash, taking the output of the enclosed command and replacing it in the outer command.
- docker ps -a -q command lists all the Docker container IDs. The -a flag lists all containers, including the ones that have stopped, and the -q flag outputs only the container IDs.
Upon execution, the above command will ensure that all containers are restarted.
Easily retrieve this command using Warp’s AI Command Suggestions
If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:
Entering docker restart all containers in the AI Command Search will prompt an docker command that can then quickly be inserted into your shell by doing CMD+ENTER.
Understanding the container restart process
When executed, the docker restart command will first send a SIGTERM signal to the container, allowing it to perform necessary cleanup tasks, such as deallocating resources, before it terminates.
If the container doesn’t terminate in the default 10 seconds timeout (which you can configure using the --time flag), Docker forcefully terminates the container by sending a SIGKILL signal.
Once the container has stopped, Docker will restart the container by creating a new instance based on the same image specified during its creation.
Note that, in between container restarts, you cannot modify the configurations of your Docker container, such as updates to port mappings or environment variables. In such cases, you need to create a new container with the updated configurations.
Moreover, when you restart a Docker container, any changes made to its file system in its previous run are lost. To retain data between container restarts, you can use Docker volumes or bind mounts, which will store data outside the container.
Sending a specific signal to the container
To send a specific signal to the container, you can use the docker restart command with the --signal flag as follows:
$ docker restart --signal <signal_name> <container …>
Run in Warp
Where:
- The signal_name specifies the name of the stop signal you want to set.
For example:
$ docker restart --signal SIGKILL mysql-db
Run in Warp
Upon execution, the above command will send a SIGKILL signal to stop the container mysql-db, instead of the default SIGTERM signal.
You can refer to this official Linux documentation to learn about signals.
Restarting containers automatically with restart policies
In Docker, a restart policy is a configuration setting that allows to determine how a container should behave when it stops.
To set a restart policy when creating a new container, you can use the docker run command with the --restart flag as follows:
$ docker run --restart=[no|always|on-failure|unless-stopped] <image_name>
Run in Warp
Where:
- The docker run command is used to create and run a new container based on the specified image_name.
- The --restart flag specifies the container’s restart policy.
- The image_name is the name of the Docker image you want to use as the base for creating your container.
For example:
$ docker run --restart=unless-stopped my_image
Run in Warp
Upon execution, the above command will create a container using the Docker image my_image and apply the unless-stopped restart policy.
Understanding restart policies
There are four restart policies that you can assign to a Docker container:
- The no policy is the default restart policy, specifying that the container won’t automatically restart when it exits.
- The on-failure[:max-retries] policy restarts the container only if it exits due to failure (non-zero exit status), with an optional limit on the number of restart attempts.
- The always policy ensures that your container will always restart regardless of its exit status. This policy helps guarantee the continuous availability of your container, however, note that if a container is in an error state, it might get stuck in a repetitive restart loop.
- The unless-stopped policy is similar to always, but it won’t restart the container if it is explicitly stopped. This policy ensures the automatic restart of the container during unexpected downtime or server issues.
For example:
$ docker run --restart=on-failure:30 my_image
Run in Warp
Upon execution, the above command will create a container using the Docker image my_image and will set the on-failure restart policy to the container.
The max-tries 30 limits the number of restart attempts. The container will attempt to restart on failure with an increasing delay, starting from 100ms and doubling with each attempt to a maximum of 30 retries.
Restarting containers manually
To get more control over stopping and starting containers, you can use the combination of the docker stop and docker start commands. This is useful when performing additional tasks such as data verification and log analysis, between stopping and starting a container.
To stop one or more containers, you can use the docker stop command as follows:
$ docker stop <container … >
Run in Warp
Which will send a SIGTERM signal to the container. Note that, if the specified container doesn't exit within 10 seconds, Docker will send a second SIGKILL signal to forcefully terminate it.
For example:
$ docker stop mysql-db
Run in Warp
Upon execution, the above command will stop the container named mysql-db.
To restart one or more stopped containers, you can then use the docker start command as follows:
$ docker start <container … >
Run in Warp
For example:
$ docker start mysql-db
Run in Warp
Upon execution, the above command will start the container named mysql-db.
Restarting unresponsive containers
If you come across unresponsive containers that you cannot restart using the other methods described in this article, you can forcefully stop them using the docker kill command as follows:
$ docker kill <container …>
Run in Warp
Which will send a SIGKILL signal to all the processes inside of the container and instantly terminate the container itself.
For example:
$ docker kill mysql-db
Run in Warp
Upon execution, the above command will forcefully stop the container named mysq-db.
Once terminated, you can restart these containers using the aforementioned docker start command.
Restarting a container every hour with Cron
To set up an automated process for periodic container restarts, you can combine the cron utility with the docker restart command.
First, you need to create a shell script containing the docker restart command for the container you want to restart:
#!/bin/bash
docker restart mysql-db
Run in Warp
Then open the cron file using the crontab command:
$ crontab -e
Run in Warp
And finally, schedule the execution of the script every hour using the following syntax:
0 * * * * /scripts/restart_container.sh
Run in Warp
Where:
- 0 * * * * specifies execution every hour.
- /scripts/restart_container.sh specifies the path to the script file to execute.
You can learn more about scheduling tasks with Cron by reading our article on how to run Cron every hour.
Written by
Mansi Manhas
Filed Under
Related Articles
Override the Container Entrypoint With docker run
Learn how to override and customize the entrypoint of a Docker container using the docker run command.
The Dockerfile ARG Instruction
Learn how to define and set build-time variables for Docker images using the ARG instruction and the --build-arg flag.
Start a Docker Container
Learn how to start a new Docker container from an image in both the foreground and the background using the docker-run command.
Stop All Docker Containers
How to gracefully shutdown running containers and forcefully kill unresponsive containers with signals in Docker using the docker-stop and docker-kill commands.
Use An .env File In Docker
Learn how to write and use .env files in Docker to populate the environment of containers on startup.
Run SSH In Docker
Learn how to launch and connect to a containerized SSH server in Docker using password-based authentication and SSH keys.
Launch MySQL Using Docker Compose
Learn how to launch a MySQL container in Docker Compose.
Execute in a Docker Container
Learn how to execute one or multiple commands in a Docker container using the docker exec command.
Expose Docker Container Ports
Learn how to publish and expose Docker container ports using the docker run command and Dockerfiles.
Restart Containers In Docker Compose
Learn how to restart and rebuild one or more containers in Docker Compose.
Output Logs in Docker Compose
Learn how to output, monitor, customize and filter the logs of the containers related to one or more services in Docker Compose
Rename A Docker Image
Learn how to rename Docker images locally and remotely using the docker tag command.