如何避免Docker容器随应用停止而终止?Postgres场景示例请求
Dockerfile to Maintain Postgres Container After Crash with Post-Mortem Checks
If you want your container to stick around after Postgres stops or crashes (so you can run environment variable and file checks), the key is to override the default entrypoint with a custom script that handles Postgres startup, waits for it to exit, runs your checks, and then keeps the container alive for inspection.
Here's a complete, ready-to-use example:
Dockerfile
# Use the official Postgres image (swap 16 for your preferred version) FROM postgres:16 # Copy our custom monitoring script into the container COPY postgres-monitor.sh /usr/local/bin/ # Make the script executable RUN chmod +x /usr/local/bin/postgres-monitor.sh # Set our script as the new entrypoint ENTRYPOINT ["/usr/local/bin/postgres-monitor.sh"]
Custom Monitoring Script (postgres-monitor.sh)
Save this file in the same directory as your Dockerfile:
#!/bin/bash # Run the official Postgres entrypoint to handle initialization and start Postgres /usr/local/bin/docker-entrypoint.sh postgres # Capture the exit code from Postgres PG_EXIT_CODE=$? echo -e "\n=====================================" echo "Postgres stopped with exit code: $PG_EXIT_CODE" echo "=====================================" # --- Environment Variable Checks --- echo -e "\n--- Relevant Environment Variables ---" # Filter for Postgres-specific vars (adjust the grep pattern to include your custom vars) printenv | grep -E "^POSTGRES_|^PG_|MY_CUSTOM_VAR" # --- File/Directory Status Checks --- echo -e "\n--- Critical File System States ---" # Check data directory permissions and ownership echo "Postgres data directory:" ls -ld /var/lib/postgresql/data/ # List key configuration files echo -e "\nCore config files:" ls -l /var/lib/postgresql/data/postgresql.conf /var/lib/postgresql/data/pg_hba.conf # Check for the postmaster PID file (indicates if Postgres crashed abruptly) echo -e "\nPostmaster PID file:" if [ -f /var/lib/postgresql/data/postmaster.pid ]; then cat /var/lib/postgresql/data/postmaster.pid else echo "No PID file found (Postgres likely shut down cleanly)" fi # Keep the container running so you can inspect it echo -e "\nContainer will stay active for post-mortem analysis." echo "Use 'docker exec -it <container-name-or-id> bash' to access the shell." tail -f /dev/null
Key Details:
- Preserves Official Postgres Functionality: The script calls the original
docker-entrypoint.shwhich handles all standard setup (like creating the initial database, setting the admin password) so you don't lose any core Postgres features. - Post-Crash Checks: After Postgres exits, the script automatically runs:
- Environment variable checks (tweak the
greppattern to include your specific variables) - File system checks (data directory permissions, config files, PID file to detect abrupt crashes)
- Environment variable checks (tweak the
- Container Persistence: The final
tail -f /dev/nullcommand keeps the container running indefinitely, allowing you to exec into it and run additional diagnostics.
How to Use:
- Save both files in the same folder.
- Build the image:
docker build -t monitored-postgres . - Run the container (adjust environment variables as needed):
docker run -d -e POSTGRES_PASSWORD=securepass --name my-postgres monitored-postgres
If Postgres stops for any reason, you'll see the check results in the container logs (docker logs my-postgres) and can still access the container shell to dig deeper.
内容的提问来源于stack exchange,提问作者Mr. Kadiev
相关产品推荐
相关产品推荐

