GeekThis remains completely ad-free with zero trackers for your convenience and privacy. If you would like to support the site, please consider giving a small contribution at Buy Me a Coffee.
Docker’s restart policy is the best way to have containers automatically start when you boot your server or computer. That should be the end of this post, but I’ll provide more details, examples, and reasons why you want to avoid other methods.
Docker’s restart policy is a flag you set when you first create a container from an image. The restart policy dictates whether the container should restart when it exits. The behavior is determined by which policy you choose. The container can automatically start again on erroneous exits or with all exit codes. The flag also affects the behavior of what happens if you manually stop a container.
Set Restart Policy
You can set the Docker restart policy when creating a container or update an existing container with a new restart policy.
The restart policy flag is
--restart and has four different options. For more details, read the official Docker Restart Policy page.
|Do not automatically restart the container. (the default)
|Restart the container if it exits due to an error, which manifests as a non-zero exit code.
|Always restart the container if it stops. If it is manually stopped, it is restarted only when Docker daemon restarts or the container itself is manually restarted.
|Similar to always, except that when the container is stopped (manually or otherwise), it is not restarted even after Docker daemon restarts.
The following command will create a container that will always restart. The majority of my containers use “always” or “unless-stopped.”
# docker run -d --restart always <image>
To update an existing container’s restart policy, use
docker update with the policy you want. The update command can modify multiple containers at the same time.
# docker update --restart unless-stopped <container> [<container> ...]
Enable Docker Service
Once you have your containers running with a restart policy, you need to enable the Docker service. If the Docker daemon doesn’t start automatically, none of your containers will start.
Depending on your system, the process to enable the Docker daemon will vary. Enable the service within systemd by using the
# systemctl enable docker
For other init systems, you’ll have to research how to enable services. It will likely be a single command you have to run or create a symbolic link.
You can verify that the service has been enabled by checking the enabled status using systemctl.
# systemctl is-enabled docker
You can get a more detailed status of the docker service by running the following systemctl command. The output will include a log to help solve any issues that may be arising.
# systemctl status docker
Avoid Systemd Services
Many posts online outline creating a systemd service file to start Docker containers. I recommend avoiding this unless you have a good reason to do so.
Docker’s built-in restart policy has better control of handling when the container should restart. The restart policy also reduces complexity in a few different ways. Firstly, it doesn’t require you to create a unique service file for each container. Secondly, all of your Docker services will be within the docker daemon and not scattered throughout the system.
I’ve written service files before for containers when I needed pre and post-tasks before and after the container was created and destroyed. Ideally, your container shouldn’t need any outside tasks. In the real world, there are times when you need to get something working until you can come up with a proper solution.