Keywords: Docker Container | Container Restart | Data Persistence | Automatic Restart Policy | Container Lifecycle
Abstract: This article provides an in-depth exploration of Docker container lifecycle management, focusing on how to properly restart stopped containers while maintaining data integrity. By comparing the differences between docker start and docker restart commands, combined with restart policy configurations, it details container state transition mechanisms. The article offers complete code examples and best practice guidelines to help developers understand container data persistence principles and avoid common configuration errors.
Fundamentals of Docker Container State Management
In the Docker ecosystem, container lifecycle management is a core operational skill. When users execute the docker run -d myimage /bin/bash -c "mycommand" command to start a container, the container enters a running state. Once mycommand completes execution, the container automatically stops, but the container instance itself remains in the system and can be viewed using the docker ps -a command.
Detailed Container Restart Mechanism
For stopped containers, the most direct restart method is using the docker start command. This command restarts the container while maintaining the original configuration parameters and data state. The specific syntax is: docker start container_name, where container_name can be either the container name or container ID.
If command execution output needs to be viewed, the -ai options can be added: docker start -ai container_name. This combination enables interactive startup, allowing users to view the output from processes within the container in real-time.
It's important to note that the docker restart command is used to restart running containers, which is fundamentally different from restarting stopped containers. Using docker restart on a stopped container may not achieve the expected results.
Data Persistence Guarantee Mechanisms
When Docker containers restart, they retain all data generated within the container by default. This is because Docker employs a Copy-on-Write mechanism where data in the writable layer remains in the storage driver after container stoppage. When the container restarts, this data is remounted, ensuring business continuity.
The following code example demonstrates the complete data persistence workflow:
# Initially start container and execute data generation command
docker run -d --name test_container ubuntu:latest /bin/bash -c "echo 'Important data' > /data/file.txt && sleep 10"
# Check container status after command completion
docker ps -a | grep test_container
# Restart container and verify data integrity
docker start test_container
docker exec test_container cat /data/file.txtAutomatic Restart Policy Configuration
Docker provides comprehensive automatic restart policies that can be configured during container creation using the --restart parameter. Main policies include:
no: No automatic restart (default)on-failure[:max-retries]: Restart only on non-zero exit codesalways: Always restart unless manually stoppedunless-stopped: Similar to always, but won't restart after manual stop
Practical application example: docker run -d --restart unless-stopped redis. This configuration ensures Redis service automatically recovers from abnormal exits while avoiding unexpected restarts after manual intervention.
Restart Policy Implementation Details
The effectiveness of restart policies depends on the prerequisite of successful container startup. Docker requires containers to run for at least 10 seconds before monitoring their status, effectively preventing failed containers from entering infinite restart loops.
For manually stopped containers, restart policies are temporarily disabled until the Docker daemon restarts or the container is manually restarted. This design avoids unnecessary resource consumption and potential configuration conflicts.
Advanced Application Scenarios
In production environments, combining volume mounts with restart policies can build highly available service architectures. For example:
# Create data volume to ensure data persistence
docker volume create app_data
# Start service container with automatic restart policy
docker run -d \
--name web_service \
--restart unless-stopped \
-v app_data:/app/data \
nginx:latestThis configuration ensures automatic recovery from service termination while guaranteeing permanent storage of critical data through data volumes.
Best Practice Recommendations
In actual operations, it's recommended to follow these principles: clearly distinguish usage scenarios between docker start and docker restart; configure appropriate restart policies for critical services; regularly monitor container status and resource usage; use data volumes or bind mounts to ensure persistent storage of important data.