From 2a60f9a29c57d35c0f07be6ec263c26b0726c42a Mon Sep 17 00:00:00 2001 From: Max Thomson Date: Tue, 30 Jul 2024 11:30:39 -0700 Subject: [PATCH] docker: add healthchecks --- Dockerfile | 4 ++-- Dockerfile.dev | 5 +++-- docker-healthcheck.sh | 18 ++++++++++++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) create mode 100755 docker-healthcheck.sh diff --git a/Dockerfile b/Dockerfile index 6146dbb17d..69b0cad8ad 100644 --- a/Dockerfile +++ b/Dockerfile @@ -80,8 +80,7 @@ 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-wrapper.sh /usr/local/bin +COPY docker-entrypoint.sh docker-wrapper.sh docker-healthcheck.sh /usr/local/bin COPY --from=gosu /usr/local/bin/gosu /usr/local/bin/gosu COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt @@ -89,5 +88,6 @@ COPY --from=builder /target/release/sqld /bin/sqld USER root +HEALTHCHECK --interval=2s CMD /usr/local/bin/docker-healthcheck.sh ENTRYPOINT ["/usr/local/bin/docker-wrapper.sh"] CMD ["/bin/sqld"] diff --git a/Dockerfile.dev b/Dockerfile.dev index 021d925baa..952569d622 100644 --- a/Dockerfile.dev +++ b/Dockerfile.dev @@ -52,6 +52,7 @@ FROM debian:bullseye-slim RUN apt update EXPOSE 5001 8080 + VOLUME [ "/var/lib/sqld" ] RUN groupadd --system --gid 666 sqld @@ -59,8 +60,7 @@ 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-wrapper.sh /usr/local/bin +COPY docker-entrypoint.sh docker-wrapper.sh docker-healthcheck.sh /usr/local/bin COPY --from=gosu /usr/local/bin/gosu /usr/local/bin/gosu COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt @@ -68,5 +68,6 @@ COPY --from=builder /sqld/bin /bin/sqld USER root +HEALTHCHECK --interval=2s CMD /usr/local/bin/docker-healthcheck.sh ENTRYPOINT ["/usr/local/bin/docker-wrapper.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