version: "3.8"
networks:
  backend:
    name: "${COMPOSE_PROJECT_NAME}"
services:
  #  haproxy:
  #    container_name: "${COMPOSE_PROJECT_NAME}.haproxy"
  #    image: haproxy:2.4-alpine
  #    ports:
  #      - 80:8080
  #      - 443:8443
  #    #extra_hosts:
  #    #  # linuxの場合、host.docker.internalが定義されていないので、
  #    #  # .bashrcとかに以下のようにLOCAL_HOST_IPを定義しておくこと
  #    #  #   export LOCAL_HOST_IP=`ip -4 a show <ethデバイス名> | grep -oP '(?<=inet\s)\d+(\.\d+){3}'`
  #    #  - host.docker.internal:${LOCAL_HOST_IP}
  #    volumes:
  #      - ./haproxy:/usr/local/etc/haproxy:ro
  nginx:
    container_name: "${COMPOSE_PROJECT_NAME}.nginx"
    build:
      context: "../../nginx"
      target: "local"
    image: "${APP_NAME}/nginx"
    ports:
      - "${WEB_PORT}:80"
    volumes:
      - ../../api/src/public:/var/www/public:cached
    depends_on:
      - api
    networks:
      - backend
  api:
    container_name: "${COMPOSE_PROJECT_NAME}.api"
    build:
      context: "../../api"
      target: "local"
    image: "${APP_NAME}/api"
    ports:
      - "9001:9000"
    volumes:
      - ../../api/src:/var/www:cached
    init: true
    networks:
      - "backend"
    depends_on:
      - "db-source"
  db-source:
    container_name: "${COMPOSE_PROJECT_NAME}.db-source"
    image: postgres:16-alpine
    command: -c 'config_file=/etc/postgresql/postgresql.conf' -c 'hba_file=/etc/postgresql/pg_hba.conf'
    restart: always
    ports:
      - "25432:5432"
    volumes:
      - db-source:/var/lib/postgresql/data
      - ./postgresql/db-source/postgresql.conf:/etc/postgresql/postgresql.conf
      - ./postgresql/db-source/pg_hba.conf:/etc/postgresql/pg_hba.conf
      - ./postgresql/db-source/init.sh:/docker-entrypoint-initdb.d/init.sh
    environment:
      POSTGRES_USER: ${DB_USER}
      POSTGRES_PASSWORD: ${DB_PASSWORD}
      POSTGRES_DB: ${DB_DATABASE}
    networks:
      - "backend"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${DB_USER} -d ${DB_DATABASE}"]
      interval: 10s
      timeout: 5s
      retries: 5
  db-replica:
    container_name: "${COMPOSE_PROJECT_NAME}.db-replica"
    image: postgres:16-alpine
    restart: always
    entrypoint: /entrypoint.sh
    ports:
      - "35432:5432"
    volumes:
      - db-replica:/var/lib/postgresql/data
      - ./postgresql/db-replica/entrypoint.sh:/entrypoint.sh
      - ./postgresql/db-replica/postgresql.conf:/etc/postgresql/postgresql.conf
      - ./postgresql/db-replica/pg_hba.conf:/etc/postgresql/pg_hba.conf
    environment:
      POSTGRES_USER: ${DB_USER}
      POSTGRES_PASSWORD: ${DB_PASSWORD}
      POSTGRES_DB: ${DB_DATABASE}
    networks:
      - "backend"
    depends_on:
      - db-source
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${DB_USER} -d ${DB_DATABASE}"]
      interval: 10s
      timeout: 5s
      retries: 5

  mailpit:
    container_name: "${COMPOSE_PROJECT_NAME}.${APP_ENV}.mailpit"
    image: "axllent/mailpit:latest"
    ports:
      - "1026:1025"
      - "8026:8025"
    environment:
      #     MP_UI_AUTH: 'pman:$$apr1$$4sxH9S4m$$icrk8MJSlwZo0RGyDQ2i8.'
      TZ: "Asia/Tokyo"
    networks:
      - "backend"
volumes:
  db-source:
  db-replica:

