Using Docker APIs to Perform Health Checks

How to Use Docker APIs to Perform Health Checks
How to Use Docker APIs to Perform Health Checks

An important aspect of creating and maintaining your production environment is performing health checks.

These are useful to ensure flawless business continuity, and make it easier to be vigilant, acting as early warning systems. These systems monitor and trigger alerts at the right time, giving us the time needed to come up with solutions and react appropriately.

In the modern era of microservices, most of the industry has moved to docker based deployments for its production environment. This enables teams to provide the horizontal scaling needed, among numerous other advantages, as compared to traditional deployment methods.

But what makes Docker based APIs so useful here, especially in situations where a service or an application needs no port to run?

The problem — Performing Health Checks on Certain Types of Microservices

There are two types of microservices:

  1. One running using some port that could be used to check the health status of the running application process.

2. The other are microservices — like scheduler services — that run at a given time of the day and perform a specific task.These services do not require a port to run.

In this context, how do we perform health checks?

>> The Solution — Use Docker APIs

The answer lies in using docker APIs for conducting health checks.

Here are the 3 simple steps to configure Docker APIs for conducting health checks:

Enable Docker API using the code below:
vi /lib/systemd/system/docker.service

Then, add the following line of code to your file:

ExecStart=/usr/bin/dockerd -H fd:// -H=tcp://0.0.0.0:5555 containerd=/run/containerd/containerd.sock $OPTIONS $DOCKER_STORAGE_OPTIONS $DOCKER_ADD_RUNTIMES

Image for post
Image for post
Enabling Docker API | Source

After enabling docker API in the previous step, shut down the Docker service and restart the same.

After performing a service restart in step 2, we should have the docker API available at the following path:

<hostname>:<port as configured above>/containers/{id or name of container}/top

Please note — We are using Docker version 18.09.9-ce, build 039a7df for this.

Verify functionality of Docker API

Looking at the container we created (named “test”), we get a 200 status code in response, if the Docker container is running alright.

Image for post
Image for post
Verifying functionality of Docker API | Source

This technique lets us get the health status of all running containers, based on their container names irrespective of whether or not an application is using a port.

In case you are using a proxy like ‘nginx’ & ‘haproxy’, you must configure these Docker APIs in that proxy service to enable health checks to be performed externally.

Please note — in our example, the name of the container is also the name of the application.

Author — Gautam Singh Malik, DLT Labs

About the Author: Gautam has many years of experience in software development. He is working with DL Asset Track as a Software Developer. He is skilled in Node.js, Linux, Core Java, Shell Scripting, Elasticsearch, Graph QL, JavaScript and Neo 4.

Written by

DLT Labs is a global leader in Distributed Ledger Technology and Enterprise Products. To know more, head over to: https://www.dltlabs.com/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store