elastic / package-registry

Elastic Package Registry (EPR)
Other
9 stars 68 forks source link

Improve flexibility of Docker HEALTHCHECK configuration #1253

Open Harmlos opened 1 week ago

Harmlos commented 1 week ago

Problem:

The current HEALTHCHECK command in the Dockerfile is inflexible, as it hardcodes the protocol (HTTP or HTTPS) and port number, making it difficult to configure dynamically based on the deployment environment. This is problematic when the protocol needs to switch between HTTP and HTTPS, or when the port number varies depending on the environment.

Example of the current configuration:

HEALTHCHECK --interval=1s --retries=30 CMD curl --silent --fail localhost:8080/health || exit 1

Issues with the current setup:

It does not support HTTPS without changing the command.
The port is hardcoded to 8080, which is not flexible.
The interval of 1 second is too short and may overload the service with frequent health checks.

Possible Solution:

To implement flexible customization, we can:

  1. Support for configurable protocol (HTTP/HTTPS) and port: By using environment variables for the protocol and port, users can configure them at runtime without modifying the Dockerfile.

    Example:

    ENV HEALTH_PORT=8080
    ENV HEALTH_PROTOCOL=http
    
    HEALTHCHECK --interval=10s --timeout=5s --retries=3 CMD sh -c 'curl --silent --fail "${HEALTH_PROTOCOL}://localhost:${HEALTH_PORT}/health" || exit 1
  2. Reasonable interval and retries: A more sensible interval of 10 seconds (instead of 1 second) for the health check, with retries set to a more reasonable number, will help ensure the service is monitored properly without unnecessary load.

Benefits:

It would also be nice to add the --insecure parameter to avoid errors when checking via HTTPS with self-signed certificates. Example:

    ENV HEALTH_PORT=8080
    ENV HEALTH_PROTOCOL=http

    HEALTHCHECK --interval=10s --timeout=5s --retries=3 CMD sh -c 'curl --silent --insecure  --fail "${HEALTH_PROTOCOL}://localhost:${HEALTH_PORT}/health" || exit 1

This change will enhance the usability of the Docker image in various environments, allowing users to configure the health check based on their specific requirements without rebuilding the image.

Perhaps you have your own ideas on how to implement a custom check so that when a container is launched with settings different from the default, the check does not break