Skip to content

Commit

Permalink
Wait for database in docker-entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
veteran29 committed Aug 14, 2023
1 parent 98dd777 commit 0090d42
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .docker/php/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,34 @@ if [ "$1" = 'php-fpm' ] || [ "$1" = 'php' ] || [ "$1" = 'bin/console' ]; then
if [ "$APP_ENV" = 'prod' ]; then
php bin/console doctrine:migrations:migrate --allow-no-migration --no-interaction
fi

if grep -q DATABASE_URL= .env; then
echo "Waiting for database to be ready..."
ATTEMPTS_LEFT_TO_REACH_DATABASE=60
until [ $ATTEMPTS_LEFT_TO_REACH_DATABASE -eq 0 ] || DATABASE_ERROR=$(php bin/console dbal:run-sql -q "SELECT 1" 2>&1); do
if [ $? -eq 255 ]; then
# If the Doctrine command exits with 255, an unrecoverable error occurred
ATTEMPTS_LEFT_TO_REACH_DATABASE=0
break
fi
sleep 1
ATTEMPTS_LEFT_TO_REACH_DATABASE=$((ATTEMPTS_LEFT_TO_REACH_DATABASE - 1))
echo "Still waiting for database to be ready... Or maybe the database is not reachable. $ATTEMPTS_LEFT_TO_REACH_DATABASE attempts left."
done

if [ $ATTEMPTS_LEFT_TO_REACH_DATABASE -eq 0 ]; then
echo "The database is not up or not reachable:"
echo "$DATABASE_ERROR"
exit 1
else
echo "The database is now ready and reachable"
fi

if [ "$( find ./migrations -iname '*.php' -print -quit )" ]; then
php bin/console doctrine:migrations:migrate --no-interaction
fi
fi

fi

exec docker-php-entrypoint "$@"

0 comments on commit 0090d42

Please sign in to comment.