-
-
Save michael-k/00b36c564a40119e55a9742e423e17aa to your computer and use it in GitHub Desktop.
Docker Health Check using Make
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM nginx | |
HEALTHCHECK --interval=3s --retries=20 CMD curl -fs http://localhost:${HTTP_PORT:-8000} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM mysql:5.7 | |
HEALTHCHECK --interval=3s --retries=20 CMD mysqlshow -u ${MYSQL_USER} -p${MYSQL_PASSWORD} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Service health functions | |
# Syntax: $(call check_service_health,<docker-compose-environment>,<service-name>) | |
get_container_id = $$(docker-compose $(1) ps -q $(2)) | |
get_container_state = $$(echo $(call get_container_id,$(1),$(2)) | xargs -I ID docker inspect -f '$(3)' ID) | |
get_service_health = $$(echo $(call get_container_state,$(1),$(2),{{if .State.Running}}{{ .State.Health.Status }}{{end}})) | |
check_service_health = { \ | |
until [[ $(call get_service_health,$(1),$(2)) != starting ]]; \ | |
do sleep 1; \ | |
done; \ | |
if [[ $(call get_service_health,$(1),$(2)) != healthy ]]; \ | |
then echo $(2) failed health check; exit 1; \ | |
fi; \ | |
} | |
# $(RELEASE_ARGS) represents the '-p <project> -f <docker-compose-file>' portion of docker-compose command | |
release: | |
# Start mysql container | |
@ docker-compose $(RELEASE_ARGS) up -d mysql | |
# This will wait until mysql container health changes from starting to healthy or unhealthy | |
@ $(call check_service_health,$(RELEASE_ARGS),mysql) | |
# Start nginx container | |
@ docker-compose $(RELEASE_ARGS) up -d nginx | |
# This will wait until nginx container health changes from starting to healthy or unhealthy | |
@ $(call check_service_health,$(RELEASE_ARGS),nginx) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment