Skip to content

Instantly share code, notes, and snippets.

@deangrant
Last active June 7, 2023 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deangrant/bff9050bc0feda801127d3ecd8b9bfb5 to your computer and use it in GitHub Desktop.
Save deangrant/bff9050bc0feda801127d3ecd8b9bfb5 to your computer and use it in GitHub Desktop.
Due to the Keycloak image being modified to enhance security and curl being removed. The below bash script provides an alternative method for providing a healthcheck. The Dockerfile and docker-compose.yml files contain the snippets to enable the healthchck
healthcheck:
test: [
"CMD-SHELL",
"bash /opt/keycloak/bin/docker-healthcheck.sh"
]
interval: 30s
timeout: 5s
retries: 3
#!/bin/bash
# Opens a network socket connection to localhost on port 8080 and associates
# it with file descriptor 3. This allows reading from and writing to the
# socket using that file descriptor.
exec 3<>/dev/tcp/localhost/8080
# Sends an HTTP GET request to the server running on localhost at port 8080.
# The request is sent through the socket using file descriptor 3.
echo -e "GET /health/ready HTTP/1.1\nhost: localhost:8080\n" >&3
# Reads the response from the socket using file descriptor 3 and prints it to
# stdout. The response is then piped to grep which searches for the string
# "status" and "UP" in the response. The grep command exits with status 0 if
# the string is found and 1 if it is not found.
timeout --preserve-status 1 cat <&3 | grep -m 1 status | grep -m 1 UP
# Saves the exit status of the previous command (grep) in the variable ERROR.
ERROR=$?
# Closes the socket. This is important because the script will hang if the
# socket is not closed.
exec 3<&-
exec 3>&-
# Exits with the same status as the previous command.
exit $ERROR
# Enable health endpoint.
ENV KC_HEALTH_ENABLED=true
# Copies the docker-healthcheck.sh file into the /opt/keycloak directory.
COPY --chown=keycloak:keycloak \
./docker-healthcheck.sh \
/opt/keycloak/bin/docker-healthcheck.sh
# Add healthcheck to execute a shell command as the health check
HEALTHCHECK --interval=30s --timeout=10s --retries=3 \
CMD-SHELL bash /opt/keycloak/bin/docker-healthcheck.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment