Skip to content

Collection of Docker Compose healthcheck examples. Tested with Docker Compose version 3.8

License

Notifications You must be signed in to change notification settings

rodrigobdz/docker-compose-healthchecks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 

Repository files navigation

Docker Compose Healthchecks

Collection of Docker Compose healthcheck examples

Collection

Postgres

healthcheck:
  test: ["CMD-SHELL", "pg_isready -U postgres"]
  interval: 30s
  timeout: 30s
  retries: 3

Source

Redis

redis:
  image: redis
  healthcheck:
    test: ["CMD", "redis-cli", "ping"]
    interval: 1s
    timeout: 3s
    retries: 30

Source

Elasticsearch

healthcheck:
  test: curl --silent http://localhost:9200 >/dev/null; if [[ $$? == 52 ]]; then echo 0; else echo 1; fi
  interval: 30s
  timeout: 10s
  retries: 5

Source

Minio RELEASE.2023-11-01T18-37-25Z and older

healthcheck:
  test: timeout 5s bash -c ':> /dev/tcp/127.0.0.1/9000' || exit 1
  start_period: 5s
  interval: 10s
  timeout: 5s
  retries: 2

Source [1]

Minio RELEASE.2023-10-25T06-33-25Z and earlier

healthcheck:
  test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
  interval: 30s
  timeout: 20s
  retries: 3

Source [1] [2]

Mongo

Note: mongosh may need to be used rather than mongo, depending on which image you're using.

healthcheck:
  test:
    [
      "CMD",
      "mongo",
      "--quiet",
      "127.0.0.1/test",
      "--eval",
      "'quit(db.runCommand({ ping: 1 }).ok ? 0 : 2)'",
    ]
  interval: 10s
  timeout: 10s
  retries: 5
  start_period: 40s

Scripting

To wait for a Docker container until its health state is healthy before executing a command inside the container, use the following function in a shell script:

readonly DOCKER_CONTAINER='my_postgres'

wait_until_container_healthy() {
  until
    [ "$(docker inspect --format "{{.State.Health.Status}}" "$DOCKER_CONTAINER")" = 'healthy' ]
  do
    # Log container status
    local container_status
    container_status="$(docker inspect --format "{{.State.Health.Status}}" "$DOCKER_CONTAINER")"

    echo "Waiting for container $DOCKER_CONTAINER to be 'healthy', current status is '$container_status'. Sleeping for five seconds..."
    sleep 5
  done
}

This snippet is an improved version of this StackOverflow answer.

Related

About

Collection of Docker Compose healthcheck examples. Tested with Docker Compose version 3.8

Topics

Resources

License

Stars

Watchers

Forks