Wait.sh — wait for hosts to be accessible or a command to return true

wait.sh is a bash script I wrote to help me set up my docker powered projects and allow me to have a functioning build process for database migrations.

The code is deeply inspired by https://github.com/vishnubob/wait-for-it and https://github.com/eficode/wait-for.

The script is able to wait for a host or multiple hosts to respond on a TCP port and can also wait for a command to output a value. For example you can wait for a file to exist or contain something.

This is mainly useful to link containers that depend on one another to start. For example you can have a container that runs install scripts that will have to wait for the database to be accessible. Or you can have a process that runs a command when the files are in place.

Multiple commands can be added as parameters and will be run sequentially.

Usage example from bash

$ ./wait.sh --wait "database_host:3306" --wait "ls -al /var/www/html | grep docker-compose.yml" --command "Database is up and files exist"

An Docker image containing this script can be accessed on DockerHub. and can be used either directly in your docker-compose file or as a base image in your Dockerfile

version: '3.3'
services:
db:
image: mysql:5.7
deploy:
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: database
wait:
image: adighe/wait
command: "./wait.sh --wait \"db:3306\" --command \"ls -al\""

The adighe/wait docker image can be used as a base image to add the wait.sh file to your own custom Docker image

Dockerfile

FROM adighe/wait as wait
FROM php:7.1.3-fpm

docker-compose.yml

version: '3.3'
services:
db:
image: mysql:5.7
deploy:
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: database
install:
build:
context: .
command: "/bin/bash -c \"/app/wait.sh --wait 'db:3306' --wait 'ls -al /var/www/html/ | grep composer.json' --command 'cd /var/www/html' --command 'ls -al' --command 'composer install' --command 'php /var/www/html/bin/console doctrine:migrations:migrate -n -vvv'\""

Backend Web Developer currently living in Bristol, UK. I mainly code in PHP and JS, but currently learning GO. I'm also very passionate about DevOps.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store