Loading…
Warp is now open-source Learn more
Loading…
To restart one or more Docker containers, you can use the docker restart command as follows:
$ docker restart <container …>Where:
For example:
$ docker restart container_one 90b8831a4b8Upon execution, the above command will restart two containers, one with the name container\_one and the other identified by its container ID 90b8831a4b8.
To restart all containers at once, you can use the docker restart command as follows:
$ docker restart $(docker ps -a -q)Where:
Upon execution, the above command will ensure that all containers are restarted.
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.
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.
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 …>Where:
For example:
$ docker restart --signal SIGKILL mysql-dbUpon 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.
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>Where:
For example:
$ docker run --restart=unless-stopped my_imageUpon execution, the above command will create a container using the Docker image my\_image and apply the unless-stopped restart policy.
There are four restart policies that you can assign to a Docker container:
For example:
$ docker run --restart=on-failure:30 my_imageUpon 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.
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 … >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-dbUpon 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 … >For example:
$ docker start mysql-dbUpon execution, the above command will start the container named mysql-db.
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 …>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-dbUpon 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.
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-dbThen open the cron file using the crontab command:
$ crontab -eAnd finally, schedule the execution of the script every hour using the following syntax:
0 * * * * /scripts/restart_container.shWhere:
You can learn more about scheduling tasks with Cron by reading our article on how to run Cron every hour.
A concise guide to learning Docker using Lazydocker. Highlights Docker’s benefits and takes advantage of Warp's AI features for a quick setup.
Learn how to launch and connect to a containerized SSH server in Docker using password-based authentication and SSH keys.
Learn how to remove a Docker image locally, on a Docker registry, and on Artifactory.
Learn how to override and customize the entrypoint of a Docker container using the docker run command.
Learn how to define and set build-time variables for Docker images using the ARG instruction and the --build-arg flag.
Learn how to start a new Docker container from an image in both the foreground and the background using the docker-run command.
How to gracefully shutdown running containers and forcefully kill unresponsive containers with signals in Docker using the docker-stop and docker-kill commands.
Learn how to set, change and match a docker container hostname.
Learn how define and pass environment variables to Docker containers using an .env file in Docker Compose.
Learn how to write and use .env files in Docker to populate the environment of containers on startup.
Start an interactive shell in Docker container
Learn how to launch a MySQL container in Docker Compose.
$ docker restart <container …>$ docker restart container_one 90b8831a4b8$ docker restart $(docker ps -a -q)$ docker restart --signal <signal_name> <container …>$ docker restart --signal SIGKILL mysql-db$ docker run --restart=[no|always|on-failure|unless-stopped] <image_name>$ docker run --restart=unless-stopped my_image$ docker run --restart=on-failure:30 my_image$ docker stop <container … >$ docker stop mysql-db$ docker start <container … >$ docker start mysql-db$ docker kill <container …>$ docker kill mysql-db#!/bin/bash
docker restart mysql-db$ crontab -e0 * * * * /scripts/restart_container.sh