I have a few Docker containers running on a DigitalOcean Droplet, and I want to make sure they automatically restart if they crash or encounter an error. I know Docker has some built-in restart policies, but I’m not entirely sure how to configure them properly.
Can someone explain how to set up Docker containers to automatically restart on failure? Also, are there any recommended restart policies for production environments?
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.
Hi there,
I’ve answered a similar question about this here:
Restart Policies: Docker provides several restart policies:
no
: The container will not be restarted (default behavior).on-failure
: Restarts the container if it exits due to an error (non-zero exit code).always
: Always restarts the container, regardless of the exit status.unless-stopped
: Restarts the container unless you manually stop it.Setting the Restart Policy: When starting a container, you can specify the restart policy with the
--restart
flag. For example, to set the container to restart on failure, use the following command:Or, to make the container always restart, use:
Example with Docker Compose: If you’re using Docker Compose, you can configure the restart policy in your
docker-compose.yml
file like this:This will ensure that your service restarts on failure.
For most production environments, using
--restart always
is a good choice to ensure that your container stays running. However, if you’re concerned about containers getting stuck in a crash loop,--restart on-failure
is a safer option to only restart the container if it fails due to an error.- Bobby
Heya,
Here’s a breakdown of the most common Docker restart policies:
Docker Restart Policies
no: This is the default policy. Docker will not restart the container automatically.
always: Docker will always restart the container if it stops, regardless of the exit status. It will also restart the container when the Docker daemon is restarted or the host machine is rebooted.
on-failure: Docker will only restart the container if it exits with a non-zero exit status (i.e., if it crashes). You can also specify a maximum number of restart attempts.
unless-stopped: Docker will always restart the container unless it was manually stopped. This is similar to the
always
policy but will not restart containers that were stopped manually.How to Apply Restart Policies
You can set the restart policy when starting a container by using the
--restart
flag in thedocker run
command. Here’s how to configure it:This example will restart the container up to 5 times if it crashes.
Recommended Restart Policies for Production
In production environments, the recommended restart policy generally depends on the application and expected behavior:
Critical applications: Use
always
orunless-stopped
to ensure that your containers stay up, even if the Docker daemon or server is restarted.Non-critical applications or testing: Use
on-failure
to limit the number of restarts in case of repeated crashes, which can help avoid potential resource exhaustion or infinite restart loops.Updating Existing Containers
To update an existing container with a restart policy, you can do so by modifying the container with the following command:
This way, you don’t have to recreate the container if it’s already running.
By implementing the appropriate restart policy, you can ensure your Docker containers are resilient and can recover from unexpected crashes or errors.