How to Start Docker Containers Automatically After a Reboot?

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:

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Bobby Iliev
Site Moderator
Site Moderator badge
December 23, 2019
Accepted Answer

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! Regards, Bobby

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! Thomas

Docker provides restart policies to control whether your containers start automatically when they exit, or when Docker restarts. Restart policies ensure that linked containers are started in the correct order. Docker recommends that you use restart policies, and avoid using process managers to start containers.

If restart policies don’t suit your needs, such as when processes outside Docker depend on Docker containers, you can use a process manager such as upstart, systemd, or supervisor instead.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel