x-erpnext: &erpnext
  image: frappe/erpnext:latest
  security_opt:
    - no-new-privileges:true
  volumes:
    - sites:/home/frappe/frappe-bench/sites
    - logs:/home/frappe/frappe-bench/logs

services:
  db:
    image: mariadb:10.6
    container_name: ERPNext-DB
    hostname: db
    security_opt:
      - no-new-privileges:true
    healthcheck:
      test: ["CMD-SHELL", "mysqladmin ping -h localhost --password=$$MARIADB_ROOT_PASSWORD"]
      interval: 10s
      timeout: 5s
      retries: 10
    command:
      - --character-set-server=utf8mb4
      - --collation-server=utf8mb4_unicode_ci
      - --skip-character-set-client-handshake
      - --skip-innodb-read-only-compressed
    environment:
      MARIADB_ROOT_PASSWORD: CHANGE-ME-DB-ROOT
    volumes:
      - /volume1/docker/erpnext/db:/var/lib/mysql:rw
    restart: on-failure:5

  redis-cache:
    image: redis:6.2-alpine
    container_name: ERPNext-REDIS-CACHE
    hostname: redis-cache
    security_opt:
      - no-new-privileges:true
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
    restart: on-failure:5

  redis-queue:
    image: redis:6.2-alpine
    container_name: ERPNext-REDIS-QUEUE
    hostname: redis-queue
    security_opt:
      - no-new-privileges:true
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
    volumes:
      - redis-queue:/data
    restart: on-failure:5

  configurator:
    <<: *erpnext
    container_name: ERPNext-CONFIGURATOR
    entrypoint: ["bash", "-c"]
    command:
      - >
        ls -1 apps > sites/apps.txt;
        bench set-config -g db_host $$DB_HOST;
        bench set-config -gp db_port $$DB_PORT;
        bench set-config -g redis_cache "redis://$$REDIS_CACHE";
        bench set-config -g redis_queue "redis://$$REDIS_QUEUE";
        bench set-config -g redis_socketio "redis://$$REDIS_QUEUE";
        bench set-config -gp socketio_port $$SOCKETIO_PORT;
    environment:
      DB_HOST: db
      DB_PORT: "3306"
      REDIS_CACHE: redis-cache:6379
      REDIS_QUEUE: redis-queue:6379
      SOCKETIO_PORT: "9000"
    depends_on:
      db:
        condition: service_healthy
      redis-cache:
        condition: service_healthy
      redis-queue:
        condition: service_healthy
    restart: "no"

  create-site:
    <<: *erpnext
    container_name: ERPNext-CREATE-SITE
    entrypoint: ["bash", "-c"]
    command:
      - >
        wait-for-it -t 120 db:3306;
        wait-for-it -t 120 redis-cache:6379;
        wait-for-it -t 120 redis-queue:6379;
        export start=`date +%s`;
        until [[ -n `grep -hs ^ sites/common_site_config.json | jq -r ".db_host // empty"` ]] &&
              [[ -n `grep -hs ^ sites/common_site_config.json | jq -r ".redis_cache // empty"` ]] &&
              [[ -n `grep -hs ^ sites/common_site_config.json | jq -r ".redis_queue // empty"` ]];
        do
          echo "Waiting for sites/common_site_config.json";
          sleep 5;
          if (( `date +%s`-start > 120 )); then
            echo "common_site_config.json not found";
            exit 1;
          fi;
        done;
        bench new-site --mariadb-user-host-login-scope='%'
        --admin-password=CHANGE-ME-ADMIN
        --db-root-username=root
        --db-root-password=CHANGE-ME-DB-ROOT
        --install-app erpnext
        --set-default frontend;
    depends_on:
      configurator:
        condition: service_completed_successfully
    restart: "no"

  backend:
    <<: *erpnext
    container_name: ERPNext-BACKEND
    hostname: backend
    depends_on:
      configurator:
        condition: service_completed_successfully
    restart: on-failure:5

  websocket:
    <<: *erpnext
    container_name: ERPNext-WEBSOCKET
    hostname: websocket
    command:
      - node
      - /home/frappe/frappe-bench/apps/frappe/socketio.js
    depends_on:
      configurator:
        condition: service_completed_successfully
    restart: on-failure:5

  queue:
    <<: *erpnext
    container_name: ERPNext-QUEUE
    hostname: queue
    command: bench worker --queue short,default,long
    depends_on:
      configurator:
        condition: service_completed_successfully
    restart: on-failure:5

  scheduler:
    <<: *erpnext
    container_name: ERPNext-SCHEDULER
    hostname: scheduler
    command: bench schedule
    depends_on:
      configurator:
        condition: service_completed_successfully
    restart: on-failure:5

  frontend:
    <<: *erpnext
    container_name: ERPNext-FRONTEND
    hostname: frontend
    command: nginx-entrypoint.sh
    ports:
      - 8087:8080
    environment:
      BACKEND: backend:8000
      SOCKETIO: websocket:9000
      FRAPPE_SITE_NAME_HEADER: frontend
      UPSTREAM_REAL_IP_ADDRESS: 127.0.0.1
      UPSTREAM_REAL_IP_HEADER: X-Forwarded-For
      UPSTREAM_REAL_IP_RECURSIVE: "off"
      PROXY_READ_TIMEOUT: "120"
      CLIENT_MAX_BODY_SIZE: 50m
    depends_on:
      - backend
      - websocket
    restart: on-failure:5

volumes:
  sites:
  logs:
  redis-queue: