- The
wait-for-containers.sh
bash script allows you to wait until a list of containers are healthy. - The script will
exit 0
if all of the supplied containers are healthy andexit 1
if any of the containers are not healthy/starting. - An optional timeout argument can be passed to the script, the default timeout is 10 minutes (600).
NOTE: The script is intended to be used with docker-compose so it will report the docker-compose service name if present over the container name, e.g. my-service
instead of project_my-service_1
.
To install wait-for-containers.sh
run the following:
curl https://gist.githubusercontent.com/JasonHewison/eafcd42c782e5bf29dbe18ac89042503/raw/a84b4c1dde0f66dad9cb12f3e5c7c55be113de4a/wait-for-containers.sh --output wait-for-containers.sh
chmod +x wait-for-containers.sh
You should now be able to run any of the following examples
docker-compose ps -q | ./wait-for-containers.sh
docker-compose ps -q | ./wait-for-containers.sh 180
echo 7d780bc23df9 | ./wait-for-containers.sh
docker ps -q | ./wait-for-containers.sh
docker ps -aq | ./wait-for-containers.sh
docker ps | grep bob | awk '{print $1}' | ./wait-for-containers.sh
docker ps --filter "label=bob" -q | ./wait-for-containers.sh
The wait-for-containers.sh
script is currently being used to help run end to end tests against a docker-compose environment with a script like the one below.
docker-compose -f docker-compose.yml -f docker-compose.hub.yml up -d
docker-compose -f docker-compose.yml -f docker-compose.hub.yml ps -q | ./wait-for-containers.sh
docker-compose -f docker-compose.yml -f docker-compose.hub.yml -f docker-compose.runner.yml run --rm runner;
docker-compose -f docker-compose.yml -f docker-compose.hub.yml stop
In the above setup with startup all of our services and a selenium hub. They are all configured with healthchecks that report healthy when the services are ready to receive requests, the database is configured and the selenium hub is fully configured and all browser nodes are registered.
wait-for-containers.sh
allows us to wait until the environment is ready before we start the test runner containined in docker-compose.runner.yml
. The runner joins the existing docker-compose environment and network so can start running e2e tests.