Keywords: Docker | Container Auto-Start | RestartPolicy
Abstract: This article explores the root cause of Docker containers auto-starting on system boot—the RestartPolicy configuration, particularly the default behavior of the 'always' policy. By analyzing the output structure of the docker inspect command, it explains how to identify a container's restart policy. The focus is on the practical use of the docker update command to disable auto-restart for containers, applicable to Docker version 1.11 and above. Additional notes on configuration considerations and best practices are included to help users effectively manage container startup behavior in their systems.
On Debian-based or other Linux systems, users may encounter Docker containers auto-starting on every system boot, even without explicit service configuration. This behavior typically stems from the container's RestartPolicy settings, rather than traditional startup scripts like cron. This article delves into this mechanism and provides practical solutions.
Core Mechanism of RestartPolicy
The auto-start behavior of Docker containers is primarily controlled by RestartPolicy. When the Docker service starts, it checks the restart policies of all containers. If the policy is set to 'always', the container will restart automatically, including during the Docker service initialization after system boot. This design ensures continuous availability of critical services but can sometimes lead to unwanted auto-starts.
To view a container's RestartPolicy, use the docker inspect command. For example, run docker inspect my-container and look for the RestartPolicy field in the output. Common policies include no (no restart), always (always restart), on-failure (restart on failure), etc. Users might have inadvertently created a container with the --restart always option, causing it to auto-start on subsequent system boots.
Solution to Disable Auto-Restart
Starting from Docker version 1.11, the docker update command can be used to easily modify a container's restart policy. To disable auto-start for a container, run the following command: docker update --restart=no my-container. This sets the RestartPolicy to no, preventing the container from auto-starting on system boot.
This method directly modifies the container's configuration without editing system files or creating additional services. It is suitable for most scenarios, especially when users initially set --restart always but later change their requirements. Ensure to use the correct container name or ID to avoid affecting other containers.
Additional Considerations
Beyond RestartPolicy, users should check for other factors that might cause auto-start. For instance, some Docker orchestration tools or custom scripts could trigger container startup. Use docker ps -a to view all container states and combine with system logs (e.g., journalctl -u docker) for diagnosis.
After modifying the restart policy, it is recommended to restart the Docker service to verify the changes: sudo systemctl restart docker. For production environments, consider using the on-failure policy as a compromise, restarting only when the container exits abnormally, balancing availability and resource management.
In summary, by understanding the RestartPolicy mechanism and leveraging the docker update command, users can effectively control container startup behavior, enhancing system management flexibility.