Skip to content

Latest commit

 

History

History
215 lines (171 loc) · 3.95 KB

README.adoc

File metadata and controls

215 lines (171 loc) · 3.95 KB

sdavids-docker-healthcheck-rust-http

A Rust-based Docker health check for an HTTP URL passed in via ENV.

ℹ️

The health check URL has to return HTTP 200.

The response body is not evaluated.

Only HTTP URLs are supported, i.e. HTTPS redirects are not supported.

💡

You can use http://captive.apple.com for testing.

This health check uses the HTTP URL passed in via the following ENV variable:

HEALTHCHECK_URL

the HTTP URL to be used for the health check

If HEALTHCHECK_URL is not set http://localhost:3000/-/health/liveness will be used.

The health check calls the URL from within the container therefore localhost is the running Docker image and not the localhost of the Docker host.

There is no check whether the given HEALTHCHECK_URL is a syntactically correct HTTP URL.

1. Development

1.1. Build

$ scripts/build.sh

target/debug/healthcheck

1.2. Test

$ scripts/test.sh

1.3. Run

$ target/debug/healthcheck
$ echo $?
0

$ HEALTHCHECK_URL=http://captive.apple.com target/debug/healthcheck
$ echo $?
0
0

the health check URL returned HTTP 200

69

the health check URL was unreachable

100

the health check URL did not return HTTP 200

1.4. Format Source Code

$ scripts/format.sh

1.5. Lint Source Code

$ scripts/lint.sh

1.6. Release Build

$ scripts/build_release.sh

target/release/healthcheck

2. Usage

  1. Copy the health check into your container:

    Dockerfile
    COPY --from=healthcheck \
      /tmp/target/release/healthcheck \
      /usr/local/bin/healthcheck
  2. Configure the health check:

    Dockerfile
    HEALTHCHECK --interval=5s --timeout=5s --start-period=5s \
        CMD healthcheck || exit 1

    More information:

  3. (Optional) Pass the HEALTHCHECK_URL to the docker container run invocation:

    scripts/docker_start.sh
    docker container run \
    ...
      --env HEALTHCHECK_URL='http://localhost:3000/-/health/liveness' \
    ...

    Alternatively, add the HEALTHCHECK_URL to the Dockerfile:

    Dockerfile
    ENV HEALTHCHECK_URL="http://localhost:3000/-/health/liveness"

3. Example

Dockerfile: a simple HTTP server

  1. Build the image:

    $ scripts/docker_build.sh
  2. Start a container:

    $ scripts/docker_start.sh
    
    Listen local: http://localhost:3000
    
    The URL has been copied to the clipboard.
  3. Examine the two endpoints:

    $ curl -s -o /dev/null -w "%{http_code}" http://localhost:3000
    200
    $ curl -s -o /dev/null -w "%{http_code}" http://localhost:3000/-/health/liveness
    200
  4. Get the health status:

    $ scripts/docker_health.sh
    healthy 0
  5. Stop the container:

    $ scripts/docker_stop.sh
  6. Remove all Docker artifacts related to this project:

    $ scripts/docker_cleanup.sh