Why Use The Docker Compose Restart Always Option

Docker Compose Restart Always
Docker Compose Restart Always

DISCLOSURE: This post may contain affiliate links, meaning when you click the links and make a purchase, we receive a commission.

Docker Compose has reduced the average developer’s stress due to its ability to configure and start multiple containers simultaneously. Where each container contains a list of options, one helpful option is restart always.

Options like these fine-tune the behavior of your container to meet the specific needs of your application and environment. This makes them incredibly useful while running containers within production environments. That said, this article focuses on the restart always option in Docker Compose and how you can use it for your benefit!

What Is The “Docker Compose Restart Always” Option

The use of the restart always option in Docker Compose allows for the continuous, reliable operation of containerized applications, ensuring that any crashes or failures are automatically addressed, and the system remains available to users.

There are two ways to use the restart always option, that is, you can either use it independently without relying on other options, or you can use it in conjunction with other options.

That said, it is important to note that the restart always option only applies to stopped or killed containers. If a container is stopped gracefully using the “docker stop” command, it will not be restarted.

Why Use The “Docker Compose Restart Always” Option

There are a number of reasons you might choose to use the restart always option in your Docker Compose configuration or when starting a container with the “docker run” command. These include:

  1. Ensuring that your application is always running, even if it crashes or encounters an error.
  2. Automatically restart the application if it is terminated for any reason.
  3. Simplifying the process of keeping the application running, as you don’t need to manually restart the container if it exits.
  4. Reducing the risk of user-facing downtime, as the application will automatically restart if it becomes unavailable.

Using the restart always option, you can help ensure that your containerized application is always available and running smoothly, even if it experiences any unexpected issues.

Combinations For The “Docker Compose Restart Always” Option

Now that we’ve discussed the option and its significance, you might be wondering about its syntax and scope. By scope, we mean its flexibility on whether the restart always option is useable with other options or not.

Portrait of young serious black businessman in glasses working at laptop in the office

However, before understanding the combinations, we hope you have the Docker and Docker Compose installed on your machine and the “docker-compose.yml” file already created for your application’s services within the root directory of your project.

The restart always option in Docker Compose can be utilized in two ways which you can use depending on your need: independently or in conjunction with other useful options. These approaches are discussed in detail below:

  • Independently

You can use the restart always option without linking it with other options, which is the simplest way of using it. To use this option in your “docker-compose.yml” file, you can add it to the “restart” key under the service definition:

version: ‘3’

services:
  web:
    image: nginx
    restart: always
    ports:
      – 80:80

This configuration would tell Docker to run a container based on the nginx image and always to restart the container if it exits. The container will also expose port 80 on the host machine and map it to port 80 inside the container, which allows the incoming HTTP traffic to reach the appropriate server successfully.

Migration backup concept copying file server

Conjunction

You can also use the Restart Always option in conjunction with other options in the “services” section of your “docker-compose.yml” file. For instance, you can use it with the “depends_on” option to specify that a container should only be started once another container is running or with the “environment” option to set environment variables in the container.

Here’s an example of a more complete “docker-compose.yml” file that uses the Restart Always option:

version: ‘3’

services:
  db:
    image: postgres
    restart: always
    environment:
      POSTGRES_USER: myuser
      POSTGRES_PASSWORD: mypass
    volumes:
      – db-data:/var/lib/postgresql/data

web:
  image: nginx
  restart: always
  depends_on:
    – db
  ports:
    – 80:80
  volumes:
    – ./www:/usr/share/nginx/html

volumes:
  db-data:

Administrator working in data center configure and check internet network on computer laptop

In the above example:

  1. The db service is based on the postgres image and is configured always to restart if it exits. It also sets some environment variables and mounts a volume.
  2. The web service is based on the nginx image, and it is also configured always to restart if it exits. It depends on the db service, so it will only be started once the db service starts running itself. The web service further exposes port 80 on the host machine and mounts a volume.

Infinite Loop: Major Issue With The “Docker Compose Restart Always” Option

It is important to note that the restart always option should be used with caution, as it can potentially create a scenario of an infinite loop. What is this problem, and how can you fix it? These things are discussed as follows:

Problem: Infinite Restarting

An infinite loop scenario can occur while using the restart always option in Docker Compose if a container is restarted indefinitely without exiting. This can happen if the container fails to start properly and is consistently restarted, but there is no termination condition to stop the Docker daemon from trying to restart the container.

  • Using Max Attempts Option To Solve Infinite Restarting

To temporarily solve the infinite loop problem, it is a good idea to set the restart_policy.max_attempts option to a non-zero value so that the Docker daemon will stop trying to restart the container if it consistently fails to start.

Computer System Data Center Content Template Graphic

To set the “restart_policy.max_attempts” option, you can include a “restart_policy” section in the “services” section of your “docker-compose.yml” file. The “max_attempts” option specifies the maximum number of times the Docker daemon will try to restart a container before giving up.

If the “max_attempts” option is set to 0, the Docker daemon will try to restart the container indefinitely. Here is an example of how you might set the “restart_policy.max_attempts” option in a “docker-compose.yml” file:

version: ‘3’

services:
  web:
    image: nginx
    restart: always
    restart_policy:
      max_attempts: 5
    ports:
      – 80:80

In the above example, the web service is based on the nginx image and is configured always to restart if it exits. The “restart_policy.max_attempts” option is set to 5, which means that the Docker daemon will try to restart the container up to 5 times if it exits. If the container consistently fails to start after 5 attempts, the Docker daemon will stop trying to restart it.

Note: It is important to note that the “restart_policy.max_attempts” option only applies if the “restart: always” option is also set. If the “restart: always” option is not set, the “restart_policy.max_attempts” option has no effect.

Engineer proceeding to data recovery from computer

Conclusion

The “restart always” option ensures that your containers are always available to serve requests. However, using this option is not always desirable, and you may use a different restart policy depending on your needs. We hope that you found our guide helpful; consider saving our blog for similar guides in the future.