Postgres docker-compose

This is a common configuration for a dockerized database

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
  db:
    image: docker.io/postgres:15-alpine
    ports: 
      - 127.0.0.1:5432:5432
    environment:
      POSTGRES_USER: databaseuser
      POSTGRES_DB: databasename
      POSTGRES_PASSWORD: secretdatabaseuser
      PGUSER: databaseuser
    healthcheck:
      test: pg_isready
      interval: 5s
      timeout: 30s
      retries: 2

Explanations:

  • ports : usually it’s suitable to set port to listen localhost. This prevents remote connections to your database
  • healthcheck: remember to configure healthcheck so you could control order of container startup.