Postgresql: How to determine if database docker container running?
On the dockerfile add:
RUN apt-get update && apt-get install -f -y postgresql-client
Then on the entrypoint script use:
while ! pg_isready -h ${MOODLE_DB_HOST} -p ${MOODLE_DB_PORT} > /dev/null 2> /dev/null; do
echo "Connecting to ${MOODLE_DB_HOST} Failed"
sleep 1
done
Another approach
An another approach is by using netcat:
for count in {1..100}; do
echo "Pinging mysql database attempt "${count}
if $(nc -z ${DB_HOST} ${DB_PORT}) ; then
echo "Can connect into database"
break
fi
sleep 5
done
Where variable ${DB_HOST}
contains the database host whilst ${DB_PORT}
contains the database port. Thas works on most databases (except if you want to detect its type as well where a custom script is needed).