Last active
September 4, 2025 00:36
-
-
Save jonas-merkle/5d6788590f4e12d99fe2aedfa62ac38f to your computer and use it in GitHub Desktop.
Docker Compose configuration for setting up a PostgreSQL container with a health check
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Docker Compose configuration for setting up a PostgreSQL container with a health check | |
| # | |
| # This configuration pulls the latest PostgreSQL image and sets up a health check | |
| # to monitor if the PostgreSQL service is ready to accept connections. The health check | |
| # uses the 'pg_isready' command to check database readiness. | |
| # | |
| # Environment variables are used to configure PostgreSQL credentials and database settings. | |
| services: | |
| postgres: | |
| image: postgres:latest # Pull the latest version of the PostgreSQL image | |
| container_name: postgres_container # Name of the PostgreSQL container | |
| hostname: postgres # Hostname for the PostgreSQL container | |
| restart: unless-stopped # Restart the container unless manually stopped | |
| # Logging configuration for PostgreSQL container | |
| logging: | |
| driver: "json-file" # Use the default json-file logging driver | |
| options: | |
| max-size: "100m" # Maximum log file size before rotation (100 MB) | |
| max-file: "10" # Maximum number of log files to retain (10) | |
| # Security options for PostgreSQL container | |
| security_opt: | |
| - no-new-privileges:true # Prevent container processes from gaining additional privileges | |
| # Uncomment and define networks if needed for the service | |
| # networks: | |
| # - application-net | |
| # Environment variables for PostgreSQL configuration | |
| environment: | |
| POSTGRES_USER: ${PG_USER:?Postgres user required!} # PostgreSQL user | |
| POSTGRES_PASSWORD: ${PG_PW:?Postgres user password required!} # PostgreSQL password | |
| POSTGRES_DB: ${PG_DB:pg-db} # PostgreSQL database name | |
| # Volume configuration for data persistence | |
| volumes: | |
| - ./data/data:/var/lib/postgresql/data # Mount the PostgreSQL data directory for persistent storage | |
| # Health check configuration to verify PostgreSQL readiness | |
| healthcheck: | |
| test: | |
| [ | |
| "CMD-SHELL", # Use the shell form to execute the health check command | |
| "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}" # Check if PostgreSQL is ready | |
| ] | |
| interval: 5s # Time between health check attempts | |
| timeout: 5s # Time to wait for a response from the health check command | |
| retries: 5 # Number of retries before marking the container as unhealthy | |
| # Container labels for additional metadata | |
| labels: | |
| - "com.centurylinklabs.watchtower.enable=true" # Enable automatic updates with Watchtower (optional) | |
| # Uncomment and define networks if needed for the service | |
| # networks: | |
| # application-net: | |
| # driver: bridge # Example of a network configuration | |
| # This health check ensures that the PostgreSQL container is only marked as healthy when the | |
| # 'pg_isready' command confirms the database is ready within the specified timeout. | |
| # If it fails after 5 attempts, the container is marked as unhealthy. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment