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:
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!
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.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
In order to enable a restart policy, you need to use the
--restart
argument when executingdocker run
.In my case what I decided to do is to use the
--restart
flag with theunless-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:As an example I also started another container without specifying the
unless-stopped
argument: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: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:Then if you run a
docker inspect
for your container and look forRestartPolicy
you should be able to see something like this:There are a few other flags that you could specify to the
--restart
argument.no
on-failure
always
unless-stopped
For more information you could take a look at the official documentation here:
https://docs.docker.com/config/containers/start-containers-automatically/
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
:systemctl list-unit-files | grep docker
showed a disabled. I manually enabled it using:Using ansible it should work this way (untested):
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.