Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mixja
Last active September 17, 2019 11:06
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mixja/1ed1314525ba4a04807303dad229f2e1 to your computer and use it in GitHub Desktop.
Save mixja/1ed1314525ba4a04807303dad229f2e1 to your computer and use it in GitHub Desktop.
Docker Health Check using Make
FROM nginx
HEALTHCHECK --interval=3s --retries=20 CMD curl -fs http://localhost:${HTTP_PORT:-8000}
FROM mysql:5.7
HEALTHCHECK --interval=3s --retries=20 CMD mysqlshow -u ${MYSQL_USER} -p${MYSQL_PASSWORD}
# 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)
@starx
Copy link

starx commented Dec 12, 2016

Some usage examples would be great to get started with for people who don't understand make (e.g me :( )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment