Docker-Compose Restart Policy – Docker

Photo of author
Written By M Ibrahim
boot2docker

Quick Fix: Utilize Docker Compose version 2, which supports restart policies using the keyword ‘restart’. Configure the ‘restart’ parameter to ‘always’ for the web service to ensure it always restarts after a crash or container exit.

The Problem:

A user wants to set a restart policy for services created using docker-compose outside of a swarm environment. However, they encountered an error indicating that the ‘restart_policy’ configuration option is not supported. The user seeks a solution or workaround to define a restart policy for their services.

The Solutions:

Solution 1: Using `restart` Keyword

  1. Version Compatibility: Use Docker Compose version 2, as it supports the restart keyword. Version 3 introduces the restart_policy parameter, but it’s only applicable when deploying to a Swarm.

  2. Syntax: In your docker-compose.yml file, add the restart keyword under the service definition:

    <!– language: lang-yaml –>

    version: ‘2’
    services:
    web:
    image: apache
    restart: always

    Replace web with the name of your service.

  3. Restart Policy Options:

    • always: Always restart the service even if Docker restarts, regardless of the exit code. This is the default behavior in Docker Compose version 2.
    • on-failure: Restart the service only if it exits with a non-zero exit code.
    • no: Never restart the service.
  4. Example:

    <!– language: lang-yaml –>

    version: ‘2’
    services:
    web:
    image: apache
    restart: on-failure

    This configures the web service to restart only if it exits with an error (non-zero exit code).

Solution 2: Using the “restart” Option

The Compose file version 3 still supports the "restart" option within the services section, similar to its behavior in version 2. This option allows you to specify the restart policy for your service containers. However, this option is not applicable when deploying services into a swarm.

Example:

version: '3'
services:
  my-service:
    restart: on-failure:5

In this example, the "restart" policy for the "my-service" service is set to "on-failure:5." This means that the container will be restarted if it exits with a non-zero exit code, and the maximum number of restart attempts is set to 5.

Valid Values for the "restart" Option:

  • no: The container will not be restarted.
  • on-failure: The container will be restarted if it exits with a non-zero exit code.
  • always: The container will be restarted regardless of the exit code.

Additional Options for the "restart" Policy:

  • delay: Specifies the delay between restart attempts in seconds. For example, "restart: on-failure:5" with "delay: 5" means that the container will be restarted if it exits with a non-zero exit code, and the delay between restart attempts will be 5 seconds.

Refer to the Docker Compose documentation for more details on the "restart" option and other restart policy settings:

Solution 3: Using –compatibility flag

Even though the restart_policy option is only officially supported for swarm deployments, there is a workaround that allows you to use it outside of swarm mode. By using the –compatibility flag, you can instruct Docker Compose to attempt to restart containers even if you’re not deploying. However, it’s important to note that the sub-keys of ‘delay’ and ‘window’ will be ignored in this scenario.

Here’s an example of how you can use the –compatibility flag with restart_policy:

version: '3.7'
services:
  build:
    context: .
    dockerfile: Dockerfile
  container_name: example
  deploy:
    restart_policy:
      condition: on-failure
      max-attempts: 3

To run this configuration, you would use the following command:

docker-compose -f docker-compose.yml --compatibility up

It’s worth noting that this workaround may not work in all cases, and it’s always best to check the official Docker documentation for the latest information on supported features.