First Time Supporter Sale: Get 50% off your paid membership - start supporting paul now!

Learn How To Successfully Implement A Healthcheck In Docker Compose

Photo by Online Marketing / Unsplash

How To Successfully Implement A Healthcheck In Docker Compose

Knowing if your Docker service is running correctly is important. A not running service can lead to a big incident! Learn how to use a Docker health check to guarantee that this does not happen to you.

Paul Knulst  in  Docker Dec 20, 2021 4 min read

1. Introduction

A health check is exactly what they sound like - a way of checking the health of a resource. In the case of Docker, a health check is used to determine the health of a running container.

When a health check command is created, it defines how a container can be tested to see if it is working correctly. With no health check defined, Docker cannot know whether or not the services running within your container are actually started or not.

While working with Docker, two ways of defining a health check exist:

  • Dockerfile
  • Docker-Compose file.

I will cover only Docker-Compose Healthchecks within this article.

2. An example using Nginx

Let’s see how health checks work by using a simple Nginx web service. To create a very simple website we need three files:

A docker-compose.very-simple-web.yml, a Dockerfile, and an index.html:

version: '3.4'
services:
  web:
    image: very-simple-web
    build:
      context: ./
      dockerfile: Dockerfile
    ports:
      - "80:80"
    restart: unless-stopped
FROM nginx
COPY html /usr/share/nginx/html
<html>
<head>
    <title>Hello World FTP</title>
</head>
<body>
  Hello World
</body>
</html>

The three files must be saved in this structure

Sample directory structure of website to build with docker-compose
Sample directory structure of website to build with docker-compose

Now let’s create the service:

docker-compose up -d --build

If you open a web browser to localhost you should see “Hello World”

3. Why do we need a health check?


🙌 Support this content

If you like this content, please consider supporting me. You can share it on social media, buy me a coffee, or become a paid member. Any support helps.

See the contribute page for all (free or paid) ways to say thank you!

Thanks! 🥰

By Paul Knulst

I'm a husband, dad, lifelong learner, tech lover, and Senior Engineer working as a Tech Lead. I write about projects and challenges in IT.