Setting the Restart Policy on a Docker Container


Docker has revolutionized the way we deploy and manage applications by offering the benefits of containerization. One of the key aspects of container management is ensuring that containers recover automatically when they fail or are stopped.

Docker provides a feature called “restart policies” to help achieve this. In this article, we will discuss what restart policies are, the different types of policies available, and how to set them for your Docker containers.

What are Docker Restart Policies?

A restart policy is a set of rules that determines how Docker should behave when a container exits. When a container stops or fails, the restart policy tells Docker whether to restart the container automatically or leave it in a stopped state. By configuring the appropriate restart policy, you can ensure that your containers recover seamlessly from failures, system reboots, or other issues, minimizing downtime and improving the reliability of your applications.

Types of Restart Policies

Docker offers four types of restart policies:

  1. no: This policy is the default and means that the container will not be restarted automatically when it stops or fails.
  2. on-failure: With this policy, Docker will restart the container only if it exits due to an error (non-zero exit status). You can also specify a maximum number of restart attempts before giving up.
  3. always: This policy ensures that the container will always be restarted, regardless of the reason for its exit. This policy is useful for containers that should always be running, such as critical services.
  4. unless-stopped: This policy is similar to the “always” policy but allows you to manually stop the container without it being restarted automatically. When the Docker daemon starts, it will restart the container unless it was stopped by the user.

Setting the Restart Policy

There are two primary ways to set the restart policy for a Docker container: during container creation or by updating an existing container.

Setting the Restart Policy During Container Creation

When creating a new container, you can set the restart policy using the “–restart” flag followed by the desired policy. For example, to create a new container with the “always” restart policy, you can use the following command:

docker run -d --name my-container --restart always my-image

Setting the Restart Policy for an Existing Container

To update the restart policy for an existing container, you need to use the “docker update” command followed by the “–restart” flag and the new policy. For example, to change the restart policy of a running container named “my-container” to “on-failure” with a maximum of 5 restart attempts, use the following command:

docker update --restart on-failure:5 my-container


Restart policies are an essential feature of Docker, allowing you to ensure the availability and reliability of your containers. By understanding the different types of restart policies and how to set them, you can enhance the resilience of your applications and minimize downtime. Whether you are running a single container or managing a complex multi-container setup, restart policies are a crucial tool for maintaining the health of your Docker environment.

Related Articles