services:
  php:
    build:
      context: ./docker/php/dev
      dockerfile: Dockerfile
    container_name: crm_siteconseil_php
    restart: unless-stopped
    group_add:
      - "${DOCKER_GID:-989}"
    volumes:
      - .:/app
      - /var/run/docker.sock:/var/run/docker.sock:ro
    ports:
      - "9000:9000"
    depends_on:
      pgbouncer:
        condition: service_healthy
      redis:
        condition: service_healthy

  database:
    image: postgres:16-alpine
    container_name: crm_siteconseil_database
    environment:
      POSTGRES_USER: app
      POSTGRES_PASSWORD: secret
      POSTGRES_DB: crm_siteconseil
    ports:
      - "5432:5432"
    volumes:
      - db-data:/var/lib/postgresql/data
      - ./docker/dovecot/init-esymail.sql:/docker-entrypoint-initdb.d/02-init-esymail.sql:ro
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U app -d crm_siteconseil"]
      interval: 5s
      timeout: 5s
      retries: 5

  pgbouncer:
    image: edoburu/pgbouncer
    container_name: crm_siteconseil_pgbouncer
    volumes:
      - ./docker/pgsql/pgbouncer-dev.ini:/etc/pgbouncer/pgbouncer.ini:ro
      - ./docker/pgsql/userlist-dev.txt:/etc/pgbouncer/userlist.txt:ro
    ports:
      - "6432:6432"
    depends_on:
      database:
        condition: service_healthy
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -h 127.0.0.1 -p 6432 -U app"]
      interval: 5s
      timeout: 5s
      retries: 5

  redis:
    image: redis:7-alpine
    container_name: crm_siteconseil_redis
    command: redis-server --requirepass crm_siteconseil
    ports:
      - "6379:6379"
    volumes:
      - redis-data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "-a", "crm_siteconseil", "ping"]
      interval: 5s
      timeout: 5s
      retries: 5

  caddy:
    image: caddy:2-alpine
    container_name: crm_siteconseil_caddy
    ports:
      - "8000:80"
    volumes:
      - ./docker/caddy/Caddyfile:/etc/caddy/Caddyfile
      - .:/app
    depends_on:
      - php

  messenger:
    build:
      context: ./docker/php/dev
      dockerfile: Dockerfile
    container_name: crm_siteconseil_messenger
    command: php bin/console messenger:consume async -vv
    restart: unless-stopped
    volumes:
      - .:/app
    depends_on:
      pgbouncer:
        condition: service_healthy
      redis:
        condition: service_healthy
      meilisearch:
        condition: service_started

  bun:
    image: oven/bun:alpine
    container_name: crm_siteconseil_bun
    working_dir: /app
    volumes:
      - .:/app
      - bun-modules:/app/node_modules
    command: sh -c "bun install && bun run dev"
    ports:
      - "5173:5173"

  mailpit:
    image: axllent/mailpit
    container_name: crm_siteconseil_mailpit
    ports:
      - "1025:1025"
      - "8025:8025"

  vault:
    image: hashicorp/vault:latest
    container_name: crm_siteconseil_vault
    cap_add:
      - IPC_LOCK
    environment:
      VAULT_DEV_ROOT_TOKEN_ID: crm_siteconseil
      VAULT_DEV_LISTEN_ADDRESS: 0.0.0.0:8200
    ports:
      - "8200:8200"
    volumes:
      - vault-data:/vault/file

  ngrok:
    image: ngrok/ngrok:latest
    container_name: crm_siteconseil_ngrok
    command: http caddy:80 --log stdout
    environment:
      NGROK_AUTHTOKEN: GXtZtKtRxRF5TFV5pCKD_25f1ALUyQQ9LkyQJgv1dr
    ports:
      - "4040:4040"
    depends_on:
      - caddy

  ngrok-sync:
    image: curlimages/curl:latest
    container_name: crm_siteconseil_ngrok_sync
    user: "0:0"
    volumes:
      - .:/app
      - ./docker/ngrok/sync.sh:/sync.sh
    depends_on:
      - ngrok
    entrypoint: sh /sync.sh

  meilisearch:
    image: getmeili/meilisearch:latest
    container_name: crm_siteconseil_meilisearch
    environment:
      MEILI_MASTER_KEY: crm_siteconseil
      MEILI_ENV: development
    ports:
      - "7700:7700"
    volumes:
      - meilisearch-data:/meili_data

  cron:
    build:
      context: ./docker/cron
      dockerfile: Dockerfile
    container_name: crm_siteconseil_cron
    restart: unless-stopped
    group_add:
      - "${DOCKER_GID:-989}"
    volumes:
      - .:/app
      - /var/run/docker.sock:/var/run/docker.sock:ro
    depends_on:
      pgbouncer:
        condition: service_healthy
      redis:
        condition: service_healthy

  redisinsight:
    image: redis/redisinsight:latest
    container_name: crm_siteconseil_redisinsight
    ports:
      - "5540:5540"
    depends_on:
      redis:
        condition: service_healthy

  postfix:
    image: boky/postfix:latest
    container_name: crm_siteconseil_postfix
    restart: unless-stopped
    hostname: mail.e-cosplay.local
    environment:
      ALLOWED_SENDER_DOMAINS: e-cosplay.fr e-cosplay.local
      RELAYHOST: "[mailpit]:1025"
      POSTFIX_myhostname: mail.e-cosplay.local
      POSTFIX_mydestination: e-cosplay.local, localhost
      POSTFIX_mynetworks: "10.0.0.0/8 172.16.0.0/12 192.168.0.0/16"
      POSTFIX_smtpd_milters: "inet:rspamd:11332"
      POSTFIX_non_smtpd_milters: "inet:rspamd:11332"
      POSTFIX_milter_protocol: "6"
      POSTFIX_milter_default_action: "accept"
      POSTFIX_smtp_tls_security_level: "none"
    volumes:
      - postfix-data:/var/spool/postfix
    depends_on:
      rspamd:
        condition: service_started
      mailpit:
        condition: service_started
    healthcheck:
      test: ["CMD-SHELL", "postfix status || exit 1"]
      interval: 10s
      timeout: 5s
      retries: 5

  rspamd:
    image: rspamd/rspamd:latest
    container_name: crm_siteconseil_rspamd
    restart: unless-stopped
    volumes:
      - rspamd-data:/var/lib/rspamd
    depends_on:
      clamav:
        condition: service_healthy
    environment:
      - CLAMAV_HOST=clamav

  clamav:
    image: clamav/clamav:stable
    container_name: crm_siteconseil_clamav
    restart: unless-stopped
    volumes:
      - clamav-data:/var/lib/clamav
    healthcheck:
      test: ["CMD-SHELL", "clamdcheck || echo 0"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 120s

  dovecot:
    build:
      context: ./docker/dovecot
      dockerfile: Dockerfile
    container_name: crm_siteconseil_dovecot
    restart: unless-stopped
    volumes:
      - dovecot-mail:/var/mail/vhosts
      - dovecot-logs:/var/log/dovecot
    depends_on:
      database:
        condition: service_healthy

  fail2ban:
    image: crazymax/fail2ban:latest
    container_name: crm_siteconseil_fail2ban
    restart: unless-stopped
    cap_add:
      - NET_ADMIN
      - NET_RAW
    network_mode: host
    volumes:
      - ./docker/fail2ban/jail.local:/etc/fail2ban/jail.local:ro
      - ./docker/fail2ban/filter.d/dovecot.conf:/etc/fail2ban/filter.d/dovecot.conf:ro
      - fail2ban-data:/var/lib/fail2ban
      - dovecot-logs:/var/log/dovecot:ro

volumes:
  db-data:
  redis-data:
  bun-modules:
  vault-data:
  meilisearch-data:
  libretranslate-data:
  postfix-data:
  rspamd-data:
  clamav-data:
  fail2ban-data:
  dovecot-mail:
  dovecot-logs:
