Keywords: Docker restart policies | Container auto-start | System integration
Abstract: This article provides an in-depth analysis of Docker container auto-start mechanisms, focusing on four restart policy modes and their application scenarios. Through detailed code examples and configuration instructions, it demonstrates how to implement container auto-restart in docker run commands and Docker Compose. It also compares system-level integration methods to help readers choose the optimal solution based on actual requirements, ensuring service high availability.
Overview of Docker Restart Policies
Docker provides built-in restart policy mechanisms that allow containers to automatically restart under specific conditions. This functionality is crucial for ensuring the continuous operation of critical services, especially in scenarios involving unexpected system reboots or abnormal container exits.
Detailed Analysis of Restart Policy Types
Docker supports four main restart policies, each suitable for different usage scenarios:
no policy: Default configuration where containers do not restart automatically after exit. Suitable for temporary tasks or development debugging environments.
on-failure policy: Automatically restarts containers when they terminate with non-zero exit codes. Maximum retry attempts can be limited using the :max-retries option. For example:
docker run -d --restart=on-failure:5 myapp
always policy: Automatically restarts containers whenever they stop, regardless of the exit reason. This is a commonly used strategy for ensuring continuous service availability:
docker run -d --restart=always redis
unless-stopped policy: Similar to the always policy, but when a container is manually stopped, it won't automatically start even after Docker daemon restart.
Configuration in Docker Compose
In Docker Compose files, restart policies can be configured using the restart field:
version: '3.8'
services:
web:
image: nginx:latest
restart: always
database:
image: postgres:13
restart: on-failure
Runtime Policy Updates
For already running containers, restart policies can be modified using the docker update command:
docker update --restart=always container_name_or_id
System-Level Integration Solutions
While Docker restart policies are sufficient in most cases, system-level integration may be necessary for specific requirements. For example, creating service unit files using systemd:
[Unit]
Description=My Application Container
Requires=docker.service
After=docker.service
[Service]
Restart=always
ExecStart=/usr/bin/docker start -a myapp_container
ExecStop=/usr/bin/docker stop -t 2 myapp_container
[Install]
WantedBy=multi-user.target
Policy Selection Recommendations
When selecting restart policies, consider the following factors: service criticality, resource consumption, and failure recovery requirements. For critical services in production environments, the always or unless-stopped policies are recommended. Avoid mixing Docker restart policies with system-level process managers to prevent conflicts.