Docker Compose containers after reboot

Viewed 10

If I run answer.apache.org on a Droplet using Docker Compose, will it still keep running if I reboot the machine?

3 Answers

Inside docker-compose.yaml I changed my restart to be unless-stopped and that worked.

version: '3'
services:
  your_service_name:
    image: your_image_name
    restart: unless-stopped
    # Other configurations, volumes, networks, etc.

A great question!

By default, a Docker container started with docker-compose will not automatically restart if the system is restarted (e.g., after a reboot).

This is because the container process is not persisted across system reboots. When a Linux system restarts, all processes, including those running containers, are terminated. The container's ephemeral storage and any data written to it are lost.

There are a few ways to handle this situation:

  1. Use a supervisor: A supervisor, like systemd or upstart, can be configured to restart a service (like a container) if it exits. This is system-specific, and the configuration depends on the distribution you're using.
  2. Use Docker Compose's own restart policy: You can configure Docker Compose to restart containers if they exit. You can add the restart option in your docker-compose.yml file:
version: '3'
services:
  apache:
    image: answer.apache.org
    restart: always

This will restart the container if it exits. You can also set it to no to prevent automatic restarts.

  1. Use a tool like docker-compose-upstart: This tool allows you to configure docker-compose services to be restarted automatically, using upstart job files. This is a separate package that needs to be installed.
  2. Use a Cloud Init configuration: If you're running on a cloud provider like DigitalOcean, you can configure Cloud Init to restart containers on boot.
  3. Configure your system's init system: Depending on your Linux distribution (e.g., Ubuntu, CentOS), you may need to configure the init system (e.g., systemd, upstart) to restart services, including containers.

Remember to consider best practices for container persistence, data storage, and backup strategies when dealing with long-running containers.

In the case of a reboot, your container will stop running, but you can configure it to restart automatically using one of the above methods.

When you use Docker Compose to orchestrate containers on a machine, the behavior after a reboot depends on several factors, including your Docker Compose configuration and Docker's restart policies. Here's a detailed answer addressing these points:

  1. Docker Compose Basics:
    Docker Compose is a tool used for defining and running multi-container Docker applications. You typically define your application services in a docker-compose.yml file.

  2. Container Restart Policies:
    Docker provides restart policies to control whether your containers should automatically restart after they exit or when Docker itself is restarted (like during a machine reboot). These policies can be set within your docker-compose.yml file under the restart key.

    Common restart policies include:

    • no - Do not automatically restart the container (default).
    • always - Always restart the container if it stops.
    • on-failure - Restart the container only if it exits with a non-zero exit code.
    • unless-stopped - Always restart the container except when the container is explicitly stopped.
  3. Setting Up Restart Policies in docker-compose.yml:
    To ensure your containers restart after a machine reboot, you can specify an appropriate restart policy (e.g., always or unless-stopped). Here is an example configuration in the docker-compose.yml file:

    version: '3'
    services:
      your_service_name:
        image: your_image_name
        restart: unless-stopped
        # Other configurations, volumes, networks, etc.
    
  4. Systemd Integration:
    If Docker is properly integrated with systemd (on systems that use it), Docker itself should automatically start when the machine reboots, and it will apply the restart policies you've defined in your docker-compose.yml to restart the containers.

  5. Docker Compose Command to Recreate and Manage Containers:
    On reboot, even though Docker restarts containers based on the restart policy, Docker Compose itself does not need to be explicitly run again unless you need to recreate or reconfigure your services. Running docker-compose up without the -d flag after a reboot doesn't necessarily do anything unless you removed the containers.

  6. Steps to Verify and Troubleshoot:

    • Check Docker Service: Ensure that the Docker daemon is set to start on boot.
      sudo systemctl enable docker
      sudo systemctl start docker
      
    • Verify Container Status: After a reboot, you can check the status of your containers.
      docker ps -a
      
    • Logs: Check logs if containers did not start as expected.
      docker logs <container_id>
      
  7. Running Docker Compose in Detached Mode:
    When you initially start your Docker Compose setup, it's common to run it in detached mode so that the containers run in the background.

    docker-compose up -d
    

Conclusion:
If you have configured the restart policies correctly in your docker-compose.yml file, and your Docker daemon is set up to start on boot, your Docker Compose containers should indeed start running automatically after a machine reboot. Adding restart: unless-stopped or restart: always to your service definitions ensures that your containers restart as expected.

If you do not see the behavior you expect, verify your configurations and inspect the Docker logs for any issues.