docker compose healthcheck code example

Example 1: healthcheck docker compose

healthcheck:
  test: ["CMD", "curl", "-f", "http://localhost"]
  interval: 1m30s
  timeout: 10s
  retries: 3
  start_period: 40s

Example 2: docker compose entrypoint

healthcheck:
  test: ["CMD", "curl", "-f", "http://localhost"]
  interval: 1m30s
  timeout: 10s
  retries: 3
  start_period: 40s

Example 3: docker compose healthcheck

healthcheck:
      start_period: 60ms
      retries: 3
      timeout: 15s
      interval: 30s
      test:
        sleep 5 &&
        touch /tmp/healthcheck.txt &&
        mkdir /tmp/healthcheck &&
        mv /tmp/healthcheck.txt /tmp/healthcheck &&
        cd /tmp/healthcheck &&
        echo "hello my docker container is working fine" >> healthcheck.txt &&
        cat healthcheck.txt &&
        chmod 777 /tmp/healthcheck &&
        rm healthcheck.txt &&
        cd .. &&
        rmdir /tmp/healthcheck &&
        clear &&
        exit