Skip to content

Docker Compose service restart policies

In Docker Compose, the restart policy is a configuration option applied to a service definition in the docker-compose.yml file^[600-developer-docker-docker-compose-docker-compouse-02.md]. This setting controls the container's behavior regarding automatic restarts upon exit or failure^[600-developer-docker-docker-compose-docker-compouse-02.md].

Syntax

The restart policy is declared as a key-value pair within the services block^[600-developer-docker-docker-compose-docker-compouse-02.md].

services:
  db:
    image: mysql:5.7
    restart: always

Policy values

The restart directive accepts specific string values that define the conditions under which the container is restarted^[600-developer-docker-docker-compose-docker-compouse-02.md].

  • no: Do not automatically restart the container.
  • always: Always restart the container. If it stops, it is restarted.
  • on-failure: Restart the container only if it exits with an error code (non-zero exit status).
  • unless-stopped: Similar to always, but ensures that containers are not restarted if they have been manually stopped (e.g., by docker-compose stop).

Sources

^[600-developer-docker-docker-compose-docker-compouse-02.md]