Skip to content

Commit

Permalink
Add a healthcheck to the Docker container image
Browse files Browse the repository at this point in the history
  • Loading branch information
nineinchnick authored and findepi committed Feb 25, 2022
1 parent 74f499b commit 50fbb46
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 2 additions & 0 deletions core/docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ EXPOSE 8080
USER trino:trino
ENV LANG en_US.UTF-8
CMD ["/usr/lib/trino/bin/run-trino"]
HEALTHCHECK --interval=10s --timeout=5s --start-period=10s \
CMD /usr/lib/trino/bin/health-check
32 changes: 32 additions & 0 deletions core/docker/bin/health-check
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash

set -euo pipefail

function get_property() {
grep "^$1=" "$2" | cut -d'=' -f2
}

scheme=http
port=8080

config=/etc/trino/config.properties
# prefer to use http even if https is enabled
if [ "$(get_property 'http-server.http.enabled' "$config")" == "false" ]; then
scheme=https
port=$(get_property http-server.https.port "$config")
else
port=$(get_property http-server.http.port "$config")
fi

endpoint="$scheme://localhost:$port/v1/info"

# add --insecure to disable certificate verification in curl, in case a self-signed certificate is being used
if ! info=$(curl --fail --silent --show-error --insecure "$endpoint"); then
echo >&2 "Server is not responding to requests"
exit 1
fi

if ! grep -q '"starting":\s*false' <<<"$info" >/dev/null; then
echo >&2 "Server is starting"
exit 1
fi
5 changes: 4 additions & 1 deletion core/docker/container-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function test_trino_starts {

set +e
I=0
until RESULT=$(docker exec "${CONTAINER_ID}" trino --execute "SELECT 'success'" 2>/dev/null); do
until docker inspect "${CONTAINER_ID}" --format "{{json .State.Health.Status }}" | grep -q '"healthy"'; do
if [[ $((I++)) -ge ${QUERY_RETRIES} ]]; then
echo "🚨 Too many retries waiting for Trino to start"
echo "Logs from ${CONTAINER_ID} follow..."
Expand All @@ -29,6 +29,9 @@ function test_trino_starts {
fi
sleep ${QUERY_PERIOD}
done
if ! RESULT=$(docker exec "${CONTAINER_ID}" trino --execute "SELECT 'success'" 2>/dev/null); then
echo "🚨 Failed to execute a query after Trino container started"
fi
set -e

cleanup
Expand Down

0 comments on commit 50fbb46

Please sign in to comment.