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.
Click below to sign up and get $200 of credit to try our products over 60 days!
Enter your email to get $200 in credit for your first 60 days with DigitalOcean.
In order to enable a restart policy, you need to use the
--restartargument when executing
In my case what I decided to do is to use the
--restartflag with the
unless-stoppedargument, 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
After that, I rebooted the host to test if this was working and sure enough the container that I started with the
unless-stoppedargument 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 updatecommand to change that:
Then if you run a
docker inspectfor your container and look for
RestartPolicyyou should be able to see something like this:
There are a few other flags that you could specify to the
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
systemctl list-unit-files | grep dockershowed 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.