Healthcheck

I am always amused on how complex K8s/Docker Swarm are, and how easy "plain" docker is.

On my humble website I use a bunch of docker containers, and I refrain to use K8s because as far as I can tell you, the effort needed to setup K8s is still huge if you have less than a dozen of services to manage and/or a easy network layout.

For instance, on my home lab I have a gitea sever which sometimes starts before its NFS disks are mounted (it is weird, but happens). I discovered you can use a special HEALTHCHECK directive inside docker or inside docker-compose to cope with it.

The healthcheck works best with Docker Swarm, but you can self-manage it if you dare to use little "kill" command :)

Docker example:

HEALTHCHECK --interval=5s --timeout=2s CMD curl --fail http://localhost || kill 1

Docker-compose example:

version: "3.4"
networks:
  gitea:
    external: false
services:
  server:
    image: gitea/gitea:1.17.3
    container_name: gitea
    environment:
      - USER_UID=1024
      - USER_GID=100
    restart: always
    networks:
      - gitea
    volumes:
      - /nfsgitaz:/data
      - /etc/timezone:/etc/timezone:ro
      - /etc/localtime:/etc/localtime:ro
    ports:
      - "80:3000"
      - "22:22"
    # To debug: docker inspect --format "\{\{json .State.Health \}\}"
    # If the NFS is not mounted we kill the service and force a restart
    healthcheck:
      test: test -d /data/git/repositories || kill -9 -1
      interval: 0m20s
      timeout: 30s
      retries: 1
      start_period: 30s

Keep in mind the restart: directive is important, to be sure docker will relaunch your just self-killed container :) Last but not least, a Stack Overflow reference

 

Last but not least, read also about an handy tool called Healthcheck.io, based on a simple yet effective way of tracing only failures