services:
  nocodb:
    image: nocodb/nocodb:latest
    restart: always
    depends_on:
      root_db:
        condition: service_healthy
    environment:
      # Reuses DB env from template.toml / .env
      NC_DB: "pg://root_db:5432?u=${POSTGRES_USER}&p=${POSTGRES_PASSWORD}&d=${POSTGRES_DB}"
    volumes:
      - nc_data:/usr/app/data

  root_db:
    image: postgres:16.6
    restart: always
    environment:
      POSTGRES_DB: ${POSTGRES_DB}
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    # More reliable healthcheck:
    healthcheck:
      test:
        [
          "CMD-SHELL",
          'pg_isready -h 127.0.0.1 -U "$POSTGRES_USER" -d "$POSTGRES_DB"',
        ]
      interval: 10s
      timeout: 5s
      start_period: 20s
      retries: 10
    volumes:
      - db_data:/var/lib/postgresql/data

volumes:
  db_data: {}
  nc_data: {}
