HomeOmer Oo

Postgres on Docker

Spin up a postgres database for your local testing

  • #docker

A docker compose file to start your app, a Postgres database and a pgadmin instance to administer the database.

It also waits for the db to be ready before starting the app.

docker-compose.yml
version: '3.9'

services:
  my_service:
    build:
      context: .
      target: prod
    restart: always
    env_file:
    - .env
    depends_on:
      db:
        condition: service_healthy

  db:
    image: postgres:14.5
    hostname: my-db
    environment:
      POSTGRES_PASSWORD: "foo"
      POSTGRES_USER: "bar"
      POSTGRES_DB: "baz"
    volumes:
    - my-db-data:/var/lib/postgresql/data
    restart: always
    healthcheck:
      test: pg_isready -U foo
      interval: 2s
      timeout: 3s
      retries: 40

  pgadmin:
    image: dpage/pgadmin4:6
    restart: always
    depends_on:
      db:
        condition: service_healthy
    environment:
      PGADMIN_DEFAULT_EMAIL: user@hello.com
      PGADMIN_DEFAULT_PASSWORD: password
    ports:
      - "5431:80"

volumes:
  my-db-data:
    name: my-db-data

Tweet this snippet

Edit on github

Omer Oo

Software engineer passionate about building the next big thing