Use pg_isready to health check the postgres container

Sample docker-compose file:

services:

  node:
    build:
      context: .
      args:
        - NODE_ENV=development
    command: npm run start
    restart: always
    ports:
      - "3000:3000"
    volumes:
      - .:/opt/node_app/app
      - ./apps/backend/src:/opt/apps/backend/src
      - ./apps/backend/src/package.json:/opt/apps/backend/src/package.json
      - ./apps/backend/src/package-lock.json:/opt/apps/backend/src/package-lock.json
      - notused:/opt/node_app/app/node_modules
    environment:
      - NODE_ENV=development
    depends_on:
      postgres:
        condition: service_healthy
    healthcheck:
      disable: true

  postgres:
    image: arm64v8/postgres:14.4-alpine
    restart: always
    environment:
      - POSTGRES_USER=test
      - POSTGRES_PASSWORD=test
      - POSTGRES_DB=test_dev
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "sh -c 'pg_isready -U test -d test_dev'"]
      interval: 10s
      timeout: 3s
      retries: 10

volumes:
  notused:

References:

Leave a comment