From 287ca1eac111d5a07e29a24632e3c52864bb9624 Mon Sep 17 00:00:00 2001 From: Max Thomson Date: Mon, 15 Jul 2024 19:26:31 -0700 Subject: [PATCH] docker: add healthchecks --- Dockerfile | 3 ++- Dockerfile.dev | 3 ++- docker-healthcheck.sh | 18 ++++++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) create mode 100755 docker-healthcheck.sh diff --git a/Dockerfile b/Dockerfile index 76a7fc9fef..ccbe880336 100644 --- a/Dockerfile +++ b/Dockerfile @@ -48,10 +48,11 @@ RUN adduser --system --home /var/lib/sqld --uid 666 --gid 666 sqld WORKDIR /var/lib/sqld USER sqld -COPY docker-entrypoint.sh /usr/local/bin +COPY docker-entrypoint.sh docker-healthcheck.sh /usr/local/bin COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt COPY --from=builder /target/release/sqld /bin/sqld +HEALTHCHECK --interval=2s CMD /usr/local/bin/docker-healthcheck.sh ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"] CMD ["/bin/sqld"] diff --git a/Dockerfile.dev b/Dockerfile.dev index 3d35414395..2809dc35f1 100644 --- a/Dockerfile.dev +++ b/Dockerfile.dev @@ -24,7 +24,7 @@ RUN apt update COPY --from=builder /sqld/bin /bin/sqld COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt -COPY docker-entrypoint.sh /usr/local/bin +COPY docker-entrypoint.sh docker-healthcheck.sh /usr/local/bin VOLUME [ "/var/lib/sqld" ] @@ -35,5 +35,6 @@ WORKDIR /var/lib/sqld EXPOSE 5001 8080 +HEALTHCHECK --interval=2s CMD /usr/local/bin/docker-healthcheck.sh ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"] CMD ["/bin/sqld"] diff --git a/docker-healthcheck.sh b/docker-healthcheck.sh new file mode 100755 index 0000000000..92cc5c285a --- /dev/null +++ b/docker-healthcheck.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash +set -euo pipefail + +SQLD_HTTP_LISTEN_ADDR="${SQLD_HTTP_LISTEN_ADDR:-"0.0.0.0:8080"}" +SQLD_HTTP_LISTEN_ADDR="${SQLD_HTTP_LISTEN_ADDR//:/\/}" + +exec 3<>"/dev/tcp/$SQLD_HTTP_LISTEN_ADDR" +echo -e "GET /health HTTP/1.1\r\nConnection: close\r\n\r\n" >&3 +RESPONSE=$(cat <&3) +exec 3<&- && exec 3>&- + +if echo "$RESPONSE" | grep -q "HTTP/1.1 200 OK"; then + exit 0 +else + echo "Did not receive HTTP 200 response" + echo "$RESPONSE" + exit 1 +fi