Docker
System Startup
Container Management
Auto-start Prevention
Troubleshooting

How to prevent docker from starting a container automatically on system startup?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Docker is an immensely powerful platform for managing and deploying applications in containers. However, in certain scenarios, you might not want some containers to automatically restart when the host system reboots. Instead, you may wish to control the start-up process of each container explicitly. This article provides a comprehensive examination of how to prevent Docker from starting a container automatically on system startup.

Understanding Docker's Container Restart Policies

Docker's behavior regarding container restarts is managed by restart policies. These policies dictate what happens to a container when it exits or the Docker daemon restarts. Here are the primary restart policies:

  • no: Do not automatically restart the container. This is the default behavior.
  • always: Always restart the container if it exits. Also, restart it when the Docker daemon restarts.
  • unless-stopped: Always restart the container except when it is manually stopped.
  • on-failure: Restart the container only if it exits with a non-zero status. This option can also be configured to limit the number of retries.

To prevent a Docker container from starting automatically on a system reboot, you would generally set its restart policy to no.

Preventing Automatic Containment with Docker CLI

When you start a Docker container, you can specify a restart policy using the --restart flag. To explicitly prevent a container from restarting automatically, you can use the following command:

bash
docker run --name sample_container --restart no <image_name>

This command starts a Docker container named sample_container from the specified <image_name> and sets the restart policy to no. Consequently, the container will not be automatically restarted when the host system reboots.

Modifying the Restart Policy for an Existing Container

If you wish to modify the restart policy for an existing container, you can achieve this using the docker update command. Here's how:

bash
docker update --restart no <container_id_or_name>

Replace <container_id_or_name> with the ID or name of your container. This command ensures that the specified container will not restart on a system reboot.

How to Inspect Current Restart Policies

To check the restart policy of a running container, you can use the docker inspect command. The output will show detailed details about the container, including its restart policy:

bash
docker inspect --format='{{.Name}} RestartPolicy: {{.HostConfig.RestartPolicy.Name}}' <container_id_or_name>

This command renders the name of the container alongside its current restart policy.

Prevent Docker Containers from Restarting by Default

If you frequently start containers that you don't want to restart, you might want to set up a system-wide default that adopts the no restart policy unless specified otherwise. This involves modifying Docker's configuration. However, there is no direct Docker daemon configuration flag for restart policies — you would enforce this at the orchestration level using scripts or configuration management tools during deployment.

Using Docker Compose

In environments managed with Docker Compose, you can specify the restart policy in your docker-compose.yml file for each service, like so:

yaml
1version: "3"
2services:
3  web:
4    image: nginx
5    restart: no

This YAML configuration ensures that the service web will not be automatically restarted.

Table: Key Points on Docker Restart Policies

FeatureDescription
no restart policyPrevents automatic restarting
always restart policyRestarts regardless of exit code
unless-stopped restart policySimilar to always but excludes manually stopped containers
on-failure[:max-retries] restart policyRestarts only on errors (non-zero exit)

Additional Considerations

  • Monitoring and Notifications: Implement monitoring solutions to notify you of container states. This way, if a crucial container is configured not to restart automatically, you'll have visibility and the potential to react promptly.
  • System Dependency Management: Investigate if automated restarts are genuinely unnecessary — ensure that they don't impact operational dependencies or runtime ecosystem needs.

Preventing automatic container restarts allows you to ensure greater control over application life cycles, which is a crucial aspect of managing services efficiently. When implementing these strategies, always consider the overarching architecture and orchestration needs of your ecosystem. By understanding and configuring Docker's restart policies effectively, you can tailor system behaviors to fit your operational requirements accurately.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design