Testing if a Docker container is healthy in a Makefile

Published on 2021-02-27 • Modified on 2021-02-27

In this snippet, we will see how to test if a Docker container is healthy in a Makefile. The goal is to use this check before running the tests, for example. They will fail if the database container is not up and ready to be used.

test: phpunit.xml check ## Run main functional and unit tests


check: ## Docker check
	@$(DOCKER) info > /dev/null 2>&1                                                                   # Docker is up
	@test '"healthy"' = `$(DOCKER) inspect --format "{{json .State.Health.Status }}" strangebuzz-db-1` # Db container is up and healthy

 More on Stackoverflow  Random snippet