Restart policies
Overview
Restart policies in Docker help ensure that containers automatically restart under specified conditions, making deployments more resilient to failures. In a docker-compose.yaml file, you can define restart policies to control how a container behaves when it stops or encounters an issue. This tutorial explains different restart policies available in Docker Compose and provides a hands-on example using the Filebrowser image.
Understanding Restart Policies
Docker provides four restart policies that can be configured in docker-compose.yaml under the restart directive:
- no: The container will not restart automatically if it stops (default behavior).
- always: Forbidden in Barbara The container will restart regardless of the reason it stopped.
- unless-stopped: The container will restart unless it is manually stopped by the user.
- on-failure: The container will restart only if it exits with a non-zero status, optionally specifying a maximum number of retries.
Restart Policies Forbidden in Barbara
The option "ALWAYS" is forbidden in Barbara Edge Nodes. Do not use it.
Example Configuration
The following docker-compose.yaml file demonstrates how to configure a restart policy for a Filebrowser container:
version: '3.8'
services:
filebrowser:
image: filebrowser/filebrowser:latest
ports:
- "8080:80"
restart: unless-stopped
Explanation:
- restart: unless-stopped ensures that the container restarts automatically after a failure or system reboot but does not restart if manually stopped.
Summary
By configuring restart policies in docker-compose.yaml, you can ensure that containers recover from failures and remain available without manual intervention. Understanding and properly setting these policies can improve the reliability of your containerized applications.