A container entrypoint (Alpine, POSIX `sh` only) must wait for Postgres before running migrations. Write the approach.
IntermediateScenarioScenarios: writing and hardening scripts
Answer
Situation read → No Bash, so no /dev/tcp, arrays or [[ ]]. The wait must be bounded, and the app must become PID 1.
What I'd do →
#!/bin/sh
set -eu
deadline=$(( $(date +%s) + ${DB_WAIT_SECONDS:-60} ))
until pg_isready -q -h "$DB_HOST" -p "${DB_PORT:-5432}"; do
if [ "$(date +%s)" -ge "$deadline" ]; then
echo "database not ready after ${DB_WAIT_SECONDS:-60}s" >&2
exit 1
fi
sleep 2
done
./migrate up
exec "$@"
pg_isreadycomes with the Postgres client package;nc -z host portis a fallback.exec "$@"hands PID 1 to the real process so it receivesSIGTERM.- Check with
shellcheck -s sh entrypoint.sh.
Why → Unbounded waits hang deploys; a missing exec breaks graceful shutdown.
Interview tip → Mention that in Kubernetes, readiness probes and init containers are often a better place for this than the entrypoint.
More on Scenarios: writing and hardening scripts
You inherit a 600-line `deploy.sh` with no error handling. It "sometimes half-deploys". Where do you start?IntermediateA script with `#!/bin/sh` works on CentOS but fails on Ubuntu with `Syntax error: "(" unexpected`. What is going on and what do you do?BeginnerYou need a deploy script that humans and CI both call with `--env`, `--version` and `--dry-run`. How do you design its interface?BeginnerA maintenance script must run as the `deploy` user, never as root, because files it creates must be owned by `deploy`. How do you enforce that?BeginnerTen scripts in a repo each copy-paste their own `log`, `die`, retry and config loading. How do you fix this?IntermediateA script is run both by engineers and by cron. Cron logs are full of colour codes and the script sometimes hangs waiting for input. Fix it.Beginner