Table of Contents
In this article, let us learn how to restart the docker container using the docker command line.
If you have a dockerized application running in production environments and suddenly the docker containers crash due to some reason, resulting in application downtime. This would cause severe problems and it would take longer to make the system functional.
In such cases, docker container restart policies would help fix the issues in production environments thereby not leading to downtime.
There are several reasons why the docker container enters terminated or exited state. The reasons maybe as follows.
- The main process gets terminated after the successful execution of tasks
- The main process gets terminated abruptly when stopped forcefully
- When the docker container is stopped
- When the docker is restarted
- When the docker container consumes too much memory
In order to reuse the docker containers which are in terminated state, we restart the docker container.
docker restart command syntax
This command helps us to restart one or more containers. Let us see how to use the command:
docker restart [OPTIONS] CONTAINER [CONTAINER...]
To restart a single container
docker restart <container_name> OR docker restart <container_id>
To restart all containers
docker restart ＄(docker ps -a -q)
docker restartcommand supports the option (
-t) to specify the number of seconds to wait to stop the container before killing it.
Let us learn how to restart the docker container with an example
Setup Lab Environment
Let us create a docker of our own which shall run for 10 seconds and exit.
#/bin/bash sleep 10 exit 1
FROM ubuntu:16.04 ADD timeout.sh /home CMD /bin/bash /home/timeout.sh
Let us build and run the custom docker
$ docker build -t test-restart:1.0 .
Simple docker restart container example
This command will run the docker container and exit after 10 seconds.
$ docker run -d test-restart:1.0
List all docker containers
$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 25e9103833eb test-restart:1.0 "/bin/sh -c '/bin/ba…" 4 minutes ago Exited (1) 4 minutes ago condescending_curran
Here we can see that the container is in an
Exited state and will remain stopped until someone restarts it.
$ docker restart 25e9103833eb
Next List the running docker containers
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 25e9103833eb test-restart:1.0 "/bin/sh -c '/bin/ba…" 9 minutes ago Up 3 seconds condescending_curran
Restarting the docker container helps to change the container state from exited to running state.
Different Container configurations
Docker provides some restart policies which tell you how the container needs to be controlled.
It helps you to start the containers in the right order and hence it is the most recommended way to use restart policies to manage all your containers.
Docker Restart Policy
Now, let us see how to use the docker restart policies.
There are a few requirements in order to successfully use the docker restart policies.
- The container must be in started state. This means that the container must be up and running for at least 10 seconds. This shall prevent docker from restarting the container unnecessarily in case it has not even started successfully.
- If you manually stop the container, then the restart policy that is set, it automatically ignored.
It shall come into effect only when the docker daemon restarts or if the container is restarted manually.
Docker Restart Policy flags
|no||This is the default option for the restart flag which means do not restart the container automatically.|
|always||This option helps to restart the container always whenever it's in a stopped state|
|unless-stopped||This option restarts the container except when the container is manually stopped or the docker is restarted.|
|On failure||This option helps you to restart the container whenever it fails with a non-zero exit code.|
Let us learn each of these methods with an example
Flag-1: Using --restart always
This options lets you restart the docker container always
$ docker run -d --restart always test-restart:1.0
List the running containers:
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 50ca135be0c6 test-restart:1.0 "/bin/sh -c '/bin/ba…" About a minute ago Up 7 seconds dazzling_roentgen $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 50ca135be0c6 test-restart:1.0 "/bin/sh -c '/bin/ba…" About a minute ago Up 3 seconds dazzling_roentgen
In the above example, we observe that the docker container is restarting continuously every time it exits.
Flag-2: Using --restart no
This is the default option which does not restart the docker container under any circumstances.
Flag-3: Using --restart on-failure
This option helps you to restart the docker container whenever it exits with a non-zero exit code. Further, you can also specify how many times the docker should automatically restart the container.
$ docker run -d --restart on-failure:2 test-restart:1.0
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d3cf0b8a9a26 test-restart:1.0 "/bin/sh -c '/bin/ba…" 31 seconds ago Restarting (1) 3 seconds ago nice_kepler 50ca135be0c6 test-restart:1.0 "/bin/sh -c '/bin/ba…" 10 minutes ago Restarting (1) 4 seconds ago dazzling_roentgen
Here you can see that the docker container restarted two times as per the max retry setting.
Flag-4: Using --restart unless-stopped
This case is similar to the always condition except that when a container is stopped or docker is restarted, the container will not be restarted.
$ docker run -d --restart unless-stopped test-restart:1.0
The output which shows the docker running
List the running docker containers
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 74a7951fb734 test-restart:1.0 "/bin/sh -c '/bin/ba…" About a minute ago Up 11 seconds epic_hamilton
Now that this container is running, let us stop it
$ docker stop 74a7951fb734 $ sudo reboot
After we restart the system, the container is still in a stopped state.
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 74a7951fb734 test-restart:1.0 "/bin/sh -c '/bin/ba…" 11 minutes ago Exited (1) 9 minutes ago epic_hamilton
In this article, we have learned about restarting the docker containers. I hope this article makes it clear to you and I suggest everyone try it out yourself.
In case you have any doubts, please feel free to add your questions in the comment section below and I shall try to respond at the earliest.