Terminus by Warp
Restart Docker Containers

Restart Docker Containers

 Mansi Manhas
Mansi Manhas

The short answer

To restart one or more Docker containers, you can use the [.inline-code]docker restart[.inline-code] command as follows:

$ docker restart <container …>

Where:

  • [.inline-code]container[.inline-code] 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

Upon execution, the above command will restart two containers, one with the name [.inline-code]container_one[.inline-code] and the other identified by its container ID [.inline-code]90b8831a4b8[.inline-code]. 

[#restart-all-containers] Restarting all containers at once [#restart-all-containers]

To restart all containers at once, you can use the [.inline-code]docker restart[.inline-code] command as follows:

$ docker restart $(docker ps -a -q)

Where:

  • [.inline-code]$(...)[.inline-code] is used for command substitution in Bash, taking the output of the enclosed command and replacing it in the outer command. 
  • [.inline-code]docker ps -a -q[.inline-code] command lists all the Docker container IDs. The [.inline-code]-a[.inline-code] flag lists all containers, including the ones that have stopped, and the [.inline-code]-q[.inline-code] flag outputs only the container IDs. 

Upon execution, the above command will ensure that all containers are restarted. 

[#easily-recall-with-ai] Easily retrieve this command using Warp’s AI Command Suggestions [#easily-recall-with-ai]

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:

Entering [.inline-code]docker restart all containers[.inline-code] in the AI Command Search will prompt an [.inline-code]docker[.inline-code] command that can then quickly be inserted into your shell by doing [.inline-code]CMD+ENTER[.inline-code].

[#understand-the-restart-process] Understanding the container restart process [#understand-the-restart-process]

When executed, the [.inline-code]docker restart[.inline-code] command will first send a [.inline-code]SIGTERM[.inline-code] 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 [.inline-code]--time[.inline-code] flag), Docker forcefully terminates the container by sending a [.inline-code]SIGKILL[.inline-code] 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 [.inline-code]docker restart[.inline-code] command with the [.inline-code]--signal[.inline-code] flag as follows: 

$ docker restart --signal <signal_name> <container …>

Where:

  • The [.inline-code]signal_name[.inline-code] specifies the name of the stop signal you want to set. 

For example:

$ docker restart --signal SIGKILL mysql-db

Upon execution, the above command will send a [.inline-code]SIGKILL[.inline-code] signal to stop the container [.inline-code]mysql-db[.inline-code], instead of the default [.inline-code]SIGTERM[.inline-code] signal. 

You can refer to this official Linux documentation to learn about signals.

[#restart-containers-automatically] Restarting containers automatically with restart policies [#restart-containers-automatically]

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 [.inline-code]docker run[.inline-code] command with the [.inline-code]--restart[.inline-code] flag as follows:

$ docker run --restart=[no|always|on-failure|unless-stopped] <image_name>

Where:

  • The [.inline-code]docker run[.inline-code] command is used to create and run a new container based on the specified [.inline-code]image_name[.inline-code]. 
  • The [.inline-code]--restart[.inline-code] flag specifies the container’s restart policy.
  • The [.inline-code]image_name[.inline-code] 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

Upon execution, the above command will create a container using the Docker image [.inline-code]my_image[.inline-code] and apply the [.inline-code]unless-stopped[.inline-code] restart policy. 

[#docker-restart-policies] Understanding restart policies [#docker-restart-policies]

There are four restart policies that you can assign to a Docker container:

  • The [.inline-code]no[.inline-code] policy is the default restart policy, specifying that the container won’t automatically restart when it exits.
  • The [.inline-code]on-failure[:max-retries][.inline-code] 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 [.inline-code]always[.inline-code] 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 [.inline-code]unless-stopped[.inline-code] policy is similar to [.inline-code]always[.inline-code], 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

Upon execution, the above command will create a container using the Docker image [.inline-code]my_image[.inline-code] and will set the [.inline-code]on-failure[.inline-code] restart policy to the container. 

The max-tries [.inline-code]30[.inline-code] 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 [.inline-code]30[.inline-code] retries.

[#restart-containers-manually] Restarting containers manually [#restart-containers-manually]

To get more control over stopping and starting containers, you can use the combination of the [.inline-code]docker stop[.inline-code] and [.inline-code]docker start[.inline-code] 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 [.inline-code]docker stop[.inline-code] command as follows:

$ docker stop <container … >

Which will send a [.inline-code]SIGTERM[.inline-code] signal to the container. Note that, if the specified container doesn't exit within 10 seconds, Docker will send a second [.inline-code]SIGKILL[.inline-code] signal to forcefully terminate it.

For example:

$ docker stop mysql-db

Upon execution, the above command will stop the container named [.inline-code]mysql-db[.inline-code].
To restart one or more stopped containers, you can then use the [.inline-code]docker start[.inline-code] command as follows:

$ docker start <container … >

For example:

$ docker start mysql-db

Upon execution, the above command will start the container named [.inline-code]mysql-db[.inline-code]. 

[#restart-unresponsive-containers] Restarting unresponsive containers [#restart-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 [.inline-code]docker kill[.inline-code] command as follows:

$ docker kill <container …>

Which will send a [.inline-code]SIGKILL[.inline-code] signal to all the processes inside of the container and instantly terminate the container itself.

For example:

$ docker kill mysql-db

Upon execution, the above command will forcefully stop the container named [.inline-code]mysq-db[.inline-code].

Once terminated, you can restart these containers using the aforementioned [.inline-code]docker start[.inline-code] command.

[#restart-containers-every-hour] Restarting a container every hour with Cron [#restart-containers-every-hour]

To set up an automated process for periodic container restarts, you can combine the [.inline-code]cron[.inline-code] utility with the [.inline-code]docker restart[.inline-code] command. 

First, you need to create a shell script containing the [.inline-code]docker restart[.inline-code] command for the container you want to restart:

#!/bin/bash

docker restart mysql-db

Then open the cron file using the [.inline-code]crontab[.inline-code] command:

$ crontab -e

And finally, schedule the execution of the script every hour using the following syntax:

0 * * * * /scripts/restart_container.sh

Where:

  • [.inline-code]0 * * * *[.inline-code] specifies execution every hour. 
  • [.inline-code]/scripts/restart_container.sh[.inline-code] 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.

Experience the power of Warp

  • Write with an IDE-style editor
  • Easily navigate through output
  • Save commands to reuse later
  • Ask Warp AI to explain or debug
  • Customize keybindings and launch configs
  • Pick from preloaded themes or design your own
brew install --cask warp
Copied!
Join the Windows waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.
Join the Linux waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.
Join the Linux waitlist or join the Windows waitlist
Join the Windows waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.