# documentation: https://invoiceninja.github.io/selfhost.html
# slogan: The leading open-source invoicing platform
# tags: invoicing, billing, accounting, finance, self-hosted
# logo: svgs/invoiceninja.png
# port: 9000

services:
  invoice-ninja:
    image: invoiceninja/invoiceninja:5
    environment:
      - SERVICE_FQDN_INVOICENINJA
      - APP_NAME=${APP_NAME:-"Invoice Ninja"}
      - APP_ENV=${APP_ENV:-production}
      - APP_URL=${SERVICE_FQDN_INVOICENINJA}
      - APP_KEY=base64:${SERVICE_REALBASE64_INVOICENINJA}
      - APP_DEBUG=${APP_DEBUG:-false}
      - REQUIRE_HTTPS=${REQUIRE_HTTPS:-false}
      - PHANTOMJS_PDF_GENERATION=${PHANTOMJS_PDF_GENERATION:-false}
      - PDF_GENERATOR=${PDF_GENERATOR:-hosted_ninja}
      - TRUSTED_PROXIES=${TRUSTED_PROXIES:-*}
      - CACHE_DRIVER=redis
      - QUEUE_CONNECTION=${QUEUE_CONNECTION:-redis}
      - SESSION_DRIVER=redis
      - REDIS_HOST=${REDIS_HOST:-redis}
      - REDIS_PASSWORD=${SERVICE_PASSWORD_REDIS}
      - REDIS_PORT=${REDIS_PORT:-6379}
      - DB_HOST=${DB_HOST:-mariadb}
      - DB_PORT=${DB_PORT:-3306}
      - DB_DATABASE=${DB_DATABASE:-invoiceninja}
      - DB_USERNAME=${SERVICE_USER_MARIADB}
      - DB_PASSWORD=${SERVICE_PASSWORD_MARIADB}
      - IN_USER_EMAIL=${IN_USER_EMAIL:-admin@example.com}
      - IN_PASSWORD=${SERVICE_PASSWORD_INVOICENINJAUSER}
      - MAIL_MAILER=${MAIL_MAILER:-log}
      - MAIL_HOST=${MAIL_HOST}
      - MAIL_PORT=${MAIL_PORT}
      - MAIL_USERNAME=${MAIL_USERNAME}
      - MAIL_PASSWORD=${MAIL_PASSWORD}
      - MAIL_ENCRYPTION=${MAIL_ENCRYPTION}
      - MAIL_FROM_ADDRESS=${MAIL_FROM_ADDRESS}
      - MAIL_FROM_NAME=${MAIL_FROM_NAME}
      - AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
      - AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
      - AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION}
      - AWS_BUCKET=${AWS_BUCKET}
      - AWS_URL=${AWS_URL}
      - AWS_ENDPOINT=${AWS_ENDPOINT}
      - NORDIGEN_SECRET_ID=${NORDIGEN_SECRET_ID}
      - NORDIGEN_SECRET_KEY=${NORDIGEN_SECRET_KEY}
      - IS_DOCKER=true
      - SCOUT_DRIVER=${SCOUT_DRIVER}
      - LICENSE_KEY=${LICENSE_KEY}
    healthcheck:
      test: ["CMD", "echo", "ok"]
      interval: 5s
      timeout: 20s
      retries: 10
    volumes:
      - invoice-ninja-public:/var/www/app/public
      - invoice-ninja-storage:/var/www/app/storage
      - type: bind
        source: ./supervisord.conf
        target: /etc/supervisord.conf
        content: |
          [supervisord]
          nodaemon=true
          pidfile=/tmp/supervisord.pid
          logfile=/dev/null ; nodaemon will cause logs to go to stdout
          logfile_maxbytes=0
          loglevel=info

          [program:php-fpm]
          redirect_stderr=true
          stdout_logfile=/dev/stdout
          stdout_logfile_maxbytes=0
          stderr_logfile=/dev/stderr
          stderr_logfile_maxbytes=0
          command=php artisan serve --host 0.0.0.0 --port 9000

          [program:scheduler]
          autorestart=true
          redirect_stderr=true
          stdout_logfile=/dev/stdout
          stdout_logfile_maxbytes=0
          stderr_logfile=/dev/stderr
          stderr_logfile_maxbytes=0
          command=php artisan schedule:work

          [program:queue-worker]
          process_name=%(program_name)s_%(process_num)02d
          autorestart=true
          redirect_stderr=true
          stdout_logfile=/dev/stdout
          stdout_logfile_maxbytes=0
          stderr_logfile=/dev/stderr
          stderr_logfile_maxbytes=0
          numprocs=2
          command=php artisan queue:work --sleep=3 --tries=1 --memory=256 --timeout=3600

          [eventlistener:shutdown]
          command=shutdown.sh
          events=PROCESS_STATE_STOPPED, PROCESS_STATE_EXITED, PROCESS_STATE_FATAL
          stdout_logfile=/dev/stdout
          stdout_logfile_maxbytes=0
          stderr_logfile=/dev/stderr
          stderr_logfile_maxbytes=0
      - type: bind
        source: ./php.ini
        target: /usr/local/etc/php/php.ini
        content: |
          session.auto_start = Off
          short_open_tag = Off

          error_reporting = E_ALL & ~E_NOTICE & ~E_WARNING & ~E_STRICT & ~E_DEPRECATED

          ; opcache.enable=1
          ; opcache.preload=/srv/www/invoiceninja/current/preload.php
          ; opcache.preload_user=www-data

          ; ; The OPcache shared memory storage size.
          ; opcache.max_accelerated_files=300000
          ; opcache.validate_timestamps=1
          ; opcache.revalidate_freq=30
          ; opcache.jit_buffer_size=256M
          ; opcache.jit=1205
          ; opcache.memory_consumption=1024M

          post_max_size = 60M
          upload_max_filesize = 50M
          memory_limit=512M
      - type: bind
        source: ./php-cli.ini
        target: /usr/local/etc/php/php-cli.ini
        content: |
          session.auto_start = Off
          short_open_tag = Off

          error_reporting = E_ALL & ~E_NOTICE & ~E_WARNING & ~E_STRICT & ~E_DEPRECATED

          ; opcache.enable_cli=1
          ; opcache.fast_shutdown=1
          ; opcache.memory_consumption=256
          ; opcache.interned_strings_buffer=8
          ; opcache.max_accelerated_files=4000
          ; opcache.revalidate_freq=60
          ; # http://symfony.com/doc/current/performance.html
          ; realpath_cache_size = 4096K
          ; realpath_cache_ttl = 600

          memory_limit = 2G
          post_max_size = 60M
          upload_max_filesize = 50M
    depends_on:
      mariadb:
        condition: service_healthy
  mariadb:
    image: mariadb:11
    volumes:
      - mariadb-data:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=${SERVICE_PASSWORD_MARIADBROOT}
      - MYSQL_DATABASE=${DB_DATABASE:-invoiceninja}
      - MYSQL_USER=${SERVICE_USER_MARIADB}
      - MYSQL_PASSWORD=${SERVICE_PASSWORD_MARIADB}
    healthcheck:
      test: ["CMD", "healthcheck.sh", "--connect", "--innodb_initialized"]
      interval: 5s
      timeout: 20s
      retries: 10

  redis:
    image: "redis:7.4-alpine"
    command: redis-server --requirepass ${SERVICE_PASSWORD_REDIS}
    environment:
      - REDIS_PASSWORD=${SERVICE_PASSWORD_REDIS}
    volumes:
      - "invoice-ninja-redis-data:/data"
    healthcheck:
      test: ["CMD", "redis-cli", "-a", "${SERVICE_PASSWORD_REDIS}", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
