docker-compose-restart-policy¶
docker-compose-restart-policy refers to the configuration setting within a docker-compose.yml file that defines the behavior of a service container when it stops or exits^[600-developer__docker__docker-compose__docker-compouse-02.md]. This policy ensures that specific services can automatically recover or maintain a specific running state based on predefined rules.
Configuration¶
In the docker-compose.yml file, the restart policy is specified using the restart directive within the service definition^[600-developer__docker__docker-compose__docker-compouse-02.md]. This allows the container to restart automatically according to the conditions defined by the chosen value.
Common Policy Values¶
One of the most frequently used policies is always. When configured with restart: always, the container is unconditionally restarted if it stops^[600-developer__docker__docker-compose__docker-compouse-02.md]. This configuration is often applied to critical background services, such as databases, to ensure continuous availability regardless of how the process terminated^[600-developer__docker__docker-compose__docker-compouse-02.md].
Example¶
The following example demonstrates a MySQL database service configured with the restart: always policy to ensure the database persists through stops or failures^[600-developer__docker__docker-compose__docker-compouse-02.md]:
version: "3"
services:
db:
image: mysql:5.7
restart: always
# ... other configuration
Related Concepts¶
- docker-compose
- Container
- [[mysql|MySQL]]