Writing a Robust Container Entrypoint Script
Scenario Prompt:
“You are writing an entrypoint script for a database migration and application startup container. It must run migrations, catch any failures immediately, clean up temporary credentials on exit, and start the application as PID 1 so it handles SIGTERM cleanly. How do you implement this?”
Substantive Technical Concepts to Hit:
Defensive Shell Flags
Set set -euo pipefail at the top of the script so any failed migration or missing environment variable aborts execution immediately.
Signal Trap and Cleanup
trap cleanup EXIT INT TERM removes temporary credential files if a migration fails. exec replaces the shell, so the trap never runs after it: delete the credentials before the exec line.
Exec Process Replacement
Start the application with exec "$@" (or exec the binary directly) so it replaces the shell process and receives SIGTERM directly as PID 1.
Parameter Expansion Defaults
Use parameter expansion such as "${PORT:-8080}" to supply a default when a variable is unset or empty.
- Does the container run as an unprivileged user where temporary files must be restricted to /tmp?
- Should migration failures trigger a retry backoff loop or immediately abort the container deployment?
- Are database credentials passed as mounted files or environment variables that need redacting from logs?