Recently one of my servers crashed and after the reboot, none of my Docker containers started.

So I had to manually check each container and start the ones that were required.

That is when I realized that I should implement a restart policy to control whether a container starts automatically or not.

Here’s what I had to do:

1 comment
  • Hi Bobbyiliev,

    After reboot my node, I wanted to restart my containers in a specific order.
    I have 3 containers, I want restart in a order one by one.
    Can you help me how to do this ?


These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

2 answers

In order to enable a restart policy, you need to use the --restart argument when executing docker run.

In my case what I decided to do is to use the --restart flag with the unless-stopped argument, that way my containers would be restarted in case that they crash or even after a reboot. Here’s an example of the command that I had to use:

docker run -dit --restart unless-stopped httpd

As an example I also started another container without specifying the unless-stopped argument:

docker run -dit httpd

After that, I rebooted the host to test if this was working and sure enough the container that I started with the unless-stopped argument started after the reboot and the second one did not:

docker ps -a
CONTAINER ID        IMAGE               COMMAND              CREATED              STATUS                          PORTS               NAMES
ab9025e61abb        httpd               "httpd-foreground"   About a minute ago   Up 26 seconds                   80/tcp              container1
4d801fec7c0c        httpd               "httpd-foreground"   About a minute ago   Exited (0) About a minute ago                       container2

If you had an already running container that you wanted to change the restart policy for, you could use the docker update command to change that:

docker update --restart unless-stopped container_id

Then if you run a docker inspect for your container and look for RestartPolicy you should be able to see something like this:

    "RestartPolicy": {
        "Name": "unless-stopped",

There are a few other flags that you could specify to the --restart argument.

Argument Description
no This is the default value, it means that the containers would not be restarted
on-failure This would restart the container in case that there is an error and the container crashes
always Always restart the container if it stops
unless-stopped The container would always be restarted unless it was manually stopped

For more information you could take a look at the official documentation here:

Here’s a quick video demo on how to do that:

Hope that this helps!

For me a problem just was also auto-starting the docker-engine itself. I didn’t do a special configeration of the docker service. After reboot the docker-engine started only after I did a docker ps:

Sep 04 04:00:03 rev5 systemd[1]: Stopped Docker Application Container Engine.
-- Reboot --
Sep 04 08:29:23 rev5 systemd[1]: Starting Docker Application Container Engine...

systemctl list-unit-files | grep docker showed a disabled. I manually enabled it using:

systemctl enable docker.service

Using ansible it should work this way (untested):

- name: Autostart service docker after reboot
    name: docker
    enabled: true

God bless!

Submit an Answer