Bobcares

Docker Swarm Health Check

by | Apr 27, 2022

The Docker swarm health check ensures that the Docker service is running properly. A faulty service can cause a major incident!

At Bobcares, we offer solutions for every query, big and small, as a part of our Docker Hosting Support.

Let’s take a look at how docker health check is used to determine the health of a container that is running.

Docker Swarm Health Check

A health check is exactly that: an examination of the health of a resource. When we create a health check command, it tells us how to test a container to see if it’s working correctly. If we don’t define a health check, Docker won’t know if the services running inside our container are actually started.

Why do we need Docker Swarm Health Check

We don’t need a health check for a simple website running on nginx. However, in a production environment, multiple processes may crash.

Because the container is running and the service is running within a Docker Swarm, the Swarm will believe that everything is fine. As a result, the Swarm believes everything is fine and will not restart the container, causing it to stop working.

There are five properties in the Docker Compose Healtcheck:

  • test: This property is the container’s health check and specifies the command that will be executed. If everything is set up correctly, this command MUST be available and working.
  • interval: This property specifies the number of seconds to wait before performing the health check, as well as the frequency with which subsequent health checks will be performed.
  • timeout: This property specifies how long Docker waits for an exit code from our health check command before declaring it failed.
  • retries: The number of consecutive health check failures required to declare a container unhealthy is specified by this property.
  • start_period: This property specifies how long our container will take to boot up. Health checks with an exit code greater than zero will not mark the container as unhealthy during this time; however, health checks with a status code of 0 will mark the container as healthy.

For example , we have created a docker-compose.very-simple-web.yml which will use curl based health check.

Please keep in mind that this health check is based on curl. To use this health check, make sure curl is installed on the image that will be used to run the service.

If curl isn’t available, another popular health check is wget, which looks like this:
wget --no-verbose --tries=1 --spider http://localhost || exit 1

Simply replace the curl command in the compose file that was previously defined.

Often used Docker swarm health checks

 

  • WGET

    healthcheck:
    test: wget --no-verbose --tries=1 --spider http://localhost || exit 1
    interval: 60s
    retries: 5
    start_period: 20s
    timeout: 10s

  • CURL

    healthcheck:
    test: curl --fail http://localhost || exit 1
    interval: 60s
    retries: 5
    start_period: 20s
    timeout: 10s

  • What should we do if we don’t have access to CURL/WGET?

    If we’re using an image that doesn’t include curl or wget, we’ll need to install them. Add an install command to our Dockerfile to accomplish this.

    curl

    RUN apk --update --no-cache add curl

    wget

    RUN apt-get update && apt-get install -y wget

    However, keep in mind that if we add curl or wget, we may also add all of their attack surfaces. A good workaround is to write our own programme, which we can then include in the docker health check test command.

    A custom Docker swarm health check

    Compared to curl or wget, the custom health check avoids all of the drawbacks of using an external tool:

  • Because we’re using the same runtime as our main app, we don’t need to install any additional prerequisites for our health check.
  • We can choose which logic to include in our health check and keep it private, so that only the Docker platform can run it.
  • The disadvantage is that we’ll have to write and maintain our own code. However, because this will be written in the same language as our app, it should be a lot easier to accomplish than writing a complex curl or wget command.

    [Looking for a solution to another query? We are just a click away.]

    Conclusion

    In short, starting a Docker container does not necessarily imply that our application is running and performing as intended. Docker health checks can be quickly implemented to identify potential software bugs before they become a major issue.

    PREVENT YOUR SERVER FROM CRASHING!

    Never again lose customers to poor server speed! Let us help you.

    Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

    GET STARTED

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.