Skip to content

Instantly share code, notes, and snippets.

@lauer
Created April 23, 2025 09:44
Show Gist options
  • Save lauer/3ee2330c8cb8af234950dfebe99e2331 to your computer and use it in GitHub Desktop.
Save lauer/3ee2330c8cb8af234950dfebe99e2331 to your computer and use it in GitHub Desktop.
simple docker entrypoint for rails project - check if database is up, from $DATABASE_URL which contains both port and hostname.
#!/bin/bash
set -e
# Extract hostname and port from DATABASE_URL
DB_HOST=$(echo $DATABASE_URL | sed -n 's/^.*[^@]*@\([^:]*\):[0-9]*.*$/\1/p')
DB_PORT=$(echo $DATABASE_URL | sed -n 's/^.*[^@]*@[^\:]*:\([0-9]*\).*$/\1/p')
# Wait for the database to be ready before starting the application
until nc -z -v -w30 $DB_HOST $DB_PORT; do
echo "Waiting for database connection..."
sleep 1
done
echo "Database is ready!"
# If running the rails server then create or migrate existing database
if [ "${1}" == "./bin/rails" ] && [ "${2}" == "server" ]; then
./bin/rails db:prepare
fi
exec "${@}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment