Docker
container
system boot
automation
startup

How do I make a Docker container start automatically on system boot?

System Design practice on Codemia

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

Practice system design

Understanding Docker Container Auto-Start on System Boot

Docker provides a robust platform for containerized applications, enabling developers to package applications into containers that can be executed in virtually any environment. One vital feature that adds to the convenience of using Docker containers is their ability to automatically start on system boot. This article will explore the detailed process of configuring Docker containers to start automatically whenever your system boots up.

Why Set Containers to Auto-Start?

Setting Docker containers to auto-start on system boot ensures high availability and reduces the need for manual intervention to start essential services. This is particularly crucial for services that need to maintain constant uptime, such as web servers, databases, and other backend services.

Methods to Enable Auto-Start for Docker Containers

Using Docker Restart Policies

Docker provides built-in restart policies that can be used to control the behavior of containers regarding starts and stops. These policies can be configured to allow a container to restart automatically under various conditions:

  1. No: The container does not restart automatically. This is the default setting.
  2. Always: The container always restarts unless explicitly stopped.
  3. Unless-stopped: The container restarts always except when explicitly stopped.
  4. On-failure: The container restarts only if it exits with a non-zero status code. An optional maximum retry count can also be specified.

Example Command:

To start a Docker container with a specific restart policy, use the --restart flag when running or creating a container:

bash
docker run --restart unless-stopped -d your_image_name
  • -d flag launches the container in detached mode.
  • your_image_name is the name of your Docker image.

Systemd Services Approach

For systems using systemd, configuring a service to manage auto-start of Docker containers provides finer control. This method is often preferred for its flexibility and robustness.

Step-by-Step Process:

  1. Create a Systemd Service Unit File:
    • Navigate to /etc/systemd/system and create a new .service file. For example: my_container.service.
  2. Edit the Service File:
    • Insert the following configuration:
 
1    [Unit]
2    Description=My Docker Container Service
3    Requires=docker.service
4    After=docker.service
5
6    [Service]
7    Restart=always
8    ExecStart=/usr/bin/docker start -a container_name
9    ExecStop=/usr/bin/docker stop -t 2 container_name
10
11    [Install]
12    WantedBy=multi-user.target
  • Replace container_name with the actual name of your Docker container.
  1. Enable the Service:
    • Use the following command to enable and start the service so that it starts at boot time:
bash
    sudo systemctl enable my_container.service
    sudo systemctl start my_container.service

Docker Compose with Up Option

If you are using Docker Compose, you can specify the restart policy directly within your docker-compose.yml file:

yaml
1version: '3'
2services:
3  web:
4    image: your_image_name
5    restart: unless-stopped

After setting up the compose file, you can start the services using:

bash
docker-compose up -d

Considerations for Auto-Start Containers

Dependencies

Make sure to define dependencies clearly if your containers depend on other services, e.g., databases or message brokers. Utilizing docker-compose allows for managing dependencies within the same stack.

Monitoring and Logs

Implement logging and monitoring to track the status of your containers. It helps in diagnosing issues if the container fails to restart due to system errors or updates.

Security Aspects

Ensure your Docker services are configured with minimal required privileges and are isolated for enhanced security.

Summary Table

Key AspectConfiguration MethodExample / Explanation
Restart PoliciesDocker CLIdocker run --restart unless-stopped -d your_image_name
Systemd ServiceSystemd unit fileCreate /etc/systemd/system/my_container.service
Docker Compose Restartdocker-compose.yml configurationrestart: unless-stopped under service section
Starting the ContainerUse systemctl or docker-composesystemctl enable my_container.service or docker-compose up -d
MonitoringEnable logging & monitoringUse tools like Docker logs or integrate with external tools
Security ConsiderationsMinimize privileges and isolateAvoid running containers with root privileges
Dependency ManagementCompose file depends_onManage and script dependencies within the same file

While configuring automatic start for Docker containers enhances operational efficiency, it is crucial to ensure they run securely and maintain optimal performance. Adapting the best practices discussed in this article will enable users to leverage Docker containers effectively and reliably.


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

All Rights Reserved.