By Bobby Iliev
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!
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:
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
:
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
service:
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.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.