Question

How to Set Up Docker Container Auto-Restart on Failure in a DigitalOcean Droplet?

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?


Submit an answer


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!

Sign In or Sign Up to Answer

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.

Bobby Iliev
Site Moderator
Site Moderator badge
October 14, 2024
Accepted Answer

Hi there,

I’ve answered a similar question about this here:

https://www.digitalocean.com/community/questions/how-to-start-docker-containers-automatically-after-a-reboot

  1. 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.
  2. 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>
    
  3. 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

KFSys
Site Moderator
Site Moderator badge
October 15, 2024

Heya,

Here’s a breakdown of the most common Docker restart policies:

Docker Restart Policies

  1. no: This is the default policy. Docker will not restart the container automatically.

  2. 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.

  3. 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.

  4. 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 the docker run command. Here’s how to configure it:

  • For a container to restart on failure:
docker run --restart on-failure:5 my-container

This example will restart the container up to 5 times if it crashes.

  • For a container to always restart:
docker run --restart always my-container
  • For a container to restart unless stopped:
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.

Updating Existing Containers

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.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more