Open In App

Docker Compose Restart Policy

Last Updated : 08 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The capability of delivering application availability and resilience in the dynamic world of software development is enormous. Docker Compose is among the powerful tools in the Docker ecosystem that make multi-container application management very easy by letting developers describe services, networks, and volumes in a single YAML file. An important aspect of maintaining application reliability is configuring appropriate restart policies for Docker containers. Restart policies define the way Docker should manage the restart of containers in case of a failure or system reboot, thus making your applications more resilient.

This article will explore Docker Compose restart policies, providing a detailed guide on their importance, types, and how to configure them effectively. With the understanding of and working with restart policies, you will be able to ensure resiliency of services, automatic recovery of unexpected failures, and maintain high availability, whether you are managing simple applications or complex microservices architectures, mastering restart policies in Docker Compose is a crucial step toward reliable and uninterrupted service delivery.

Primary Terminologies

Docker

  • Docker is a platform that enables developers to create, deploy, and manage applications in containers through automation, containers package an application and its dependencies to run consistently from one computing environment to another.

Docker Compose

  • Docker Compose is a tool used for defining and running multi-container Docker applications. It enables the configuration of services, networks, and volumes in your application with the help of a YAML file. This eases an all-in-one YAML setup for the whole service stack, network, and volume configurations of a developed application in terms of management and orchestration.

Container

  • A Container is a lightweight, stand-alone, executable package that includes everything needed to run software: code, runtime, system tools, libraries, and settings. Containers ensure that software runs the same, regardless of the operating environment.

Restart Policy

  • The restart policy parameter allows Docker Compose to indicate how a container should behave when it is restarted if it stops or crashes. Below is the configuration of the restart policy that ensures your services are up and automatically recover from any failure.

YAML

  • YAML (YAML Ain't Markup Language) is a human-readable data serialization format commonly used for writing configuration files. Compose uses YAML for configuration of services, networks, and volumes.

Service

  • Within Docker Compose, each Service definition describes a particular application or microservice running within a container. For multi-container application definitions, each service is defined in the docker-compose.yml file.

Volumes

  • The volumes in Docker help to persist storage where data can be saved and shared between the containers. They are defined within a Docker Compose file to save or retain the data through every restart of a container and during the re-deployment of this particular container.

Dockerfile

  • A Dockerfile is a script in which the steps and the process to build a Docker image are defined. It defines the parent or base image, application source code, other dependencies, runtime commands, and configurations to execute the application.

Orchestration

  • Orchestration within the Docker space involves automating the ordering, coordination, and management of complex containerized applications, docker Compose is an orchestration tool that makes it easier to manage multi-container applications.

What is Docker Compose Restart Policy?

A Docker Compose restart policy is a configuration setting for Docker to use in determining how to restart containers that have stopped or failed. This ensures the recovery of unexpected shutdowns or crashes of any container so that the services keep running as planned. The definitions of the restart policies are set within a docker-compose.yml file for every service so that the behavior for every container can be easily customized based on its role and criticality.

Types of Restart policies

Docker Compose defines different restart policies according to the situations that have taken place:

  • no: It will not be automatically restarted if it stops. This is the default behavior unless a restart policy has been specified.
  • always: The container will be restarted always, no matter the exit status. This ensures maximum service availability and suits only the critical services that must not fail.
  • on-failure: This will restart the container only if it has exited with a non-zero exit code, hence encountering an error. This is useful for services that should be restarted only when they experience failure.
  • unless-stopped: This will restart the container unless it is explicitly stopped or Docker itself is stopped or restarted. This policy is the same as always but allows for manual intervention to stop the container without it automatically restarting.

Step-by-Step Process to Configuring Restart Policies in Docker Compose

Step 1: Launch EC2 instance

  • Go to AWS Console and Navigate to EC2 Instance dashboard launch an ec2 instance
Launch EC2 instance

Step 2: Install Docker

  • Now install docker in our local machine by using following commands
sudo yum -y install docker
Install Docker
  • Start and enable docker by using following commands
sudo systemctl start docker
sudo systemctl enable docker
sudo systemctl status docker
Screenshot-2024-06-23-142555

Step 3: Install Docker Compose

  • Now install docker compose in our local machine
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
Install Docker Compose

Step 4: Create docker-compose.yml File

  • Define your services in a docker-compose.yml file, including the restart policy for each service.

Specify the restart policy:

The restart policy is configured with the restart key inside each service definition. There are four policies available:

  • no: Do not automatically restart the container (default).
  • always: Always restart the container if it stops.
  • on-failure: Restart the container if it exits with a non-zero exit code.
  • unless-stopped: Always restart the container unless it is explicitly stopped or Docker itself is stopped or restarted.

Here is the docker-compose file

version: '3'
services:
web:
image: nginx:alpine
ports:
- "80:80"
restart: always
db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: example
restart: on-failure

Example with Explanations

  • web service: Runs an NGINX server and will always restart if it stops.
  • db service: Runs a MySQL database and will restart only if it exits with an error.
Create docker-compose.yml File


Step 5: Run the Docker Compose

Start the Services:

  • Use the docker-compose up -d command to start the services defined in docker-compose.yml file.
docker-compose up -d
Run the Docker Compose

Step 6: Check the Restart Policy

  • Use docker inspect to verify the restart policy of a running container.
docker inspect -f "{{.HostConfig.RestartPolicy.Name}}" <container_name>
Check the Restart Policy
  • Here we see that nginx server restart policy was always
  • mysql server restart policy was on-failure

Conclusion

One of the essential points that will help to gain resilience and reliability for applications deployed in containers is understanding and configuring restart policies. These policies provide proper control to ensure services are running and able to recover automatically after failures, crashes, or reboots. Proper restart policies for each service, as described in docker-compose.yml, give a possibility to tailor service behavior to its concrete availability and operational requirements.

Always, on-failure and unless-stopped restart policies can help you refine how Docker handles container restart so you can keep those crucial services flexible for debugging and maintenance. With perfect configurations, this can lead to significant reductions in downtimes, significant improvements in robustness, better user experiences, and more reliable deployments.

Adding restart policies to your Docker Compose configuration is a proactive move toward building resilient and self-healing applications. Whether you manage anything from a small web server to a vast microservices architecture, these policies are vital in maintaining continuous and non-disruptive service delivery. Understanding Docker Compose restart policies takes your skills in deploying and managing applications to another level because you feel more confident about their resilience and recovery from random disruptions.


Next Article
Article Tags :

Similar Reads