version: "2.1"
services:
  postgres:
    restart: always
    image: "postgres:15.1-alpine"
    container_name: phoenixDB
    networks:
      - backend
    ports:
      - "5432:5432"
    environment:
      - DEBUG=false
      - POSTGRES_DB=phoenix
      - "POSTGRES_PASSWORD=${POSTGRES_PASSWORD}"
    volumes:
      - "./database:/var/lib/postgresql/data"
    healthcheck:
      test:
        - CMD-SHELL
        - pg_isready -U postgres
      interval: 5s
      timeout: 2s
      retries: 5
  pgadmin:
    image: dpage/pgadmin4
    container_name: pgadmin_container
    networks:
      - backend
      - frontend
    environment:
      PGADMIN_DEFAULT_EMAIL: "pgadmin4@pgadmin.org"
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD}
      PGADMIN_CONFIG_SERVER_MODE: 'False'
    ports:
      - "${PGADMIN_PORT:-5050}:80"
    volumes:
      - "pgadmin:/var/lib/pgadmin"
  phoenix-app:
    restart: always
    image: "yurimatoslima/phoenix-frontend:alpha"
    container_name: phoenixApp
    volumes:
      - "./app_custom:/usr/share/nginx/html/assets/custom"
    networks:
      - backend # primary network [external]
      - frontend # internal network
    depends_on:
      - phoenix-system
  phoenix-system:
    restart: always
    image: "yurimatoslima/phoenix-backend:alpha"
    environment:
      - "DB_HOST=phoenixDB"
      - "DB_PASSWORD=${POSTGRES_PASSWORD}"
      - "DB_USERNAME=postgres"
      - "SUPER_ADMIN_USER_PASSWORD=${SUPER_ADMIN_USER_PASSWORD}"
      - "REDIS_PASSWORD=${REDIS_PASSWORD}"
      - RUN_JOB_QUEUE=${RUN_JOB_QUEUE}
      - SMTP_FROM=${SMTP_FROM}
      - SMTP_TYPE=${SMTP_TYPE}
      - SMTP_NAME=${SMTP_NAME}
      - SMTP_HOST=${SMTP_HOST}
      - SMTP_PORT=${SMTP_PORT}
      - SMTP_SECURE=${SMTP_SECURE}
      - SMTP_USER=${SMTP_USER}
      - SMTP_PASS=${SMTP_PASS}
      - SMTP_LOGGING=${SMTP_LOGGING}
      - SMTP_DEBUG=${SMTP_DEBUG}
      - SMTP_TLS_REJECT_UNAUTHORIZED=${SMTP_TLS_REJECT_UNAUTHORIZED}
      - SMTP_SECURE_CONNECTION=${SMTP_SECURE_CONNECTION}
      - ENV_MODE=${ENV_MODE}
      - SMTP_TLS_CIPHERS={SMTP_TLS_CIPHERS}
    command: ["npm", "run", "start:server"]
    deploy:
      replicas: 1 #change here if u want to have more replicas. Cant find a way to set via variable right now
    networks:
      - backend
    depends_on:
      postgres:
        condition: service_healthy
      phoenix-redis:
        condition: service_healthy
    volumes:
      - "./assets:/usr/src/app/packages/dev-server/assets"
      - "./server_custom:/usr/src/app/packages/dev-server/custom"
  phoenix-worker:
    restart: always
    image: "yurimatoslima/phoenix-backend:alpha"
    networks:
      - backend
    environment:
      - DB_HOST=phoenixDB
      - "DB_PASSWORD=${POSTGRES_PASSWORD}"
      - DB_USERNAME=postgres
      - "SUPER_ADMIN_USER_PASSWORD=${SUPER_ADMIN_USER_PASSWORD}"
      - REDIS_PASSWORD=${REDIS_PASSWORD}
    command: ["npm", "run", "start:worker"]
    depends_on:
      postgres:
        condition: service_healthy
    volumes:
      - "./assets:/usr/src/app/packages/dev-server/assets"
      - "./server_custom:/usr/src/app/packages/dev-server/custom"
  phoenix-redis:
    image: 'bitnami/redis:latest'
    container_name: redis
    command: /opt/bitnami/scripts/redis/run.sh --maxmemory 100mb
    user: root
    networks:
      - backend
    restart: always
    environment:
      ALLOW_EMPTY_PASSWORD: "no"
      REDIS_DISABLE_COMMANDS: FLUSHDB,FLUSHALL,CONFIG
      REDIS_PASSWORD: ${REDIS_PASSWORD}
    healthcheck:
      test: [ "CMD", "redis-cli", "--raw", "incr", "ping" ]
    volumes:
      - "./redis/data:/bitnami/redis/data"
  https_portal:
    container_name: https_portal
    image: "steveltn/https-portal:1.21"
    restart: unless-stopped
    user: "root"
    networks:
      - frontend # [ PgAdmin, Phoenix-App ]
      - external # [ Outside of the World]
    ports:
      - "80:80"
      - "443:443"
      # - host:container
    environment:
      STAGE: "production" # Use Let's Encrypt production server
      WEBSOCKET: "true" # Enable websocket support
      DEBUG: "true"
      RENEW_MARGIN_DAYS: 30
      CLIENT_MAX_BODY_SIZE: 0
      # FORCE_RENEW: 'true'
      DOMAINS:
    volumes:
      - ./https_portal/data:/var/lib/https-portal # ssl_certs, vhost.d, htdocs
      - ./https_portal/log:/var/log/nginx # nginx logs
    depends_on:
      - phoenix-app
      - phoenix-system
      - pgadmin
      - phoenix-redis
      - postgres

networks:
  backend:
    driver: bridge
    external: false

  frontend:
    driver: bridge
    external: false

  external:
    driver: bridge
    external: true

volumes:
  pgadmin: null
