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!
Accepted Answer
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:
docker run --restart on-failure <image_name>
Or, to make the container always restart, use:
docker run --restart always <image_name>
Example with Docker Compose:
If you’re using Docker Compose, you can configure the restart policy in your docker-compose.yml
file like this:
version: '3'
services:
myservice:
image: my-image
restart: on-failure
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:
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.
You can set the restart policy when starting a container by using the --restart
flag in the docker run
command. Here’s how to configure it:
docker run --restart on-failure:5 my-container
This example will restart the container up to 5 times if it crashes.
docker run --restart always my-container
docker run --restart unless-stopped my-container
In production environments, the recommended restart policy generally depends on the application and expected behavior:
Critical applications: Use always
or unless-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.
To update an existing container with a restart policy, you can do so by modifying the container with the following command:
docker update --restart=always my-container
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.
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.