Docker

How to Successfully Implement A Healthcheck In Docker Compose

The healthcheck in Docker is a way to check the health of Docker containers. This feature was not available in previous versions of Docker. The health check command determines whether the container is working and executing the application. Sometimes, in a container, deadlock conditions can occur while running the application in containers. There may be a situation where the container is self-removed, but the containerized process is still running in an infinite loop, or some resources are not functioning properly in the container. To keep a check on these kinds of situations, the healthcheck property is utilized.

This article will illustrate the method for implementation of a healthcheck in Docker compose.

How to Implement a Healthcheck in Docker Compose?

The healthcheck in Docker compose is used to identify the container’s health condition to execute the service. To implement a healthcheck in Docker compose, go through the listed steps.

Step 1: Make Dockerfile

First, create a Dockerfile to containerize your application. For instance, we have specified the instructions to dockerize the “index.html” file:

FROM nginx:latest
COPY index.html /usr/share/nginx/html/index.html
ENTRYPOINT ["nginx", "-g", "daemon off;"]

 

Step 2: Create Compose File

Next, make a Yaml file named “docker-compose.yml”. This file is frequently used to configure the multiple services of applications in a container.

Here, we have used the following instructions:

  • version” is used to specify the compose file version.
  • services” is utilized to configure the application services in the container.
  • web” is a service of application or project.
  • ports”: key allocates the container’s exposed port:
version: "3"
services:
  web:
    build: .
    ports:
      - 80:80

 

Step 3: Fire up the Container

Fire up the container by utilizing the “docker-compose up” command:

docker-compose up

 

Navigate to the exposing port of the container and check if the container is executing or not:

Step 4: Implement the Health Check

In the next step, modify the “docker-compose.yml” file to implement the health check in the compose container. For this purpose, utilize the following properties:

  • healthcheck” is used to implement the health check.
  • test” key is used to test the container. For this purpose, we have used the “curl” command to get a response or signals from the host.
  • interval” specifies the time duration or interval in which the healthcheck process will execute.
  • timeout” defines the time duration to wait for a healthcheck. In case of an error or some unusual condition, after the specified time, it will return the exit code.
  • retries” is used to define the number of tries to implement the health check after failure:
version: "3"
services:
  web:
    build: .
    ports:
      - 80:80
    healthcheck:
      test: curl --fail http://localhost || exit 1
      interval: 30s
      timeout: 10s
      retries: 5

 

Step 5: Start Container

Again start the containers:

docker-compose up

 

After that defined time interval, the health check process will be implemented and check the health of container as highlighted below:

Step 6: Check Health Status

In order to check the container’s health condition, list down the compose container. Here, you can see the condition of our running container:

docker-compose ps -a

 

The output indicates that our container is in a healthy condition:

This is all about checking the health condition of the container in Docker compose.

Conclusion

To implement the Health Check in Docker-compose, first, create a “docker-compose.yml” file and configure the application services. After that, utilize the “healthcheck” property to implement the health check. This property uses different keys to implement the healthcheck, such as “interval”, “timeout”, “retries”, and “test”. This article has illustrated the method to implement the healthcheck in Docker compose.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.