version: '3.5'

networks:
  frontend:
    driver: ${NETWORKS_DRIVER}
  backend:
    driver: ${NETWORKS_DRIVER}
volumes:
  mysql:
    driver: ${VOLUMES_DRIVER}
  redis:
    driver: ${VOLUMES_DRIVER}
  smtp:
    driver: ${VOLUMES_DRIVER}
  phpmyadmin:
    driver: ${VOLUMES_DRIVER}
  docker-in-docker:
    driver: ${VOLUMES_DRIVER}

services:
### Workspace Utilities ##################################
    workspace:
      restart: always
      build:
        context: ./workspace
        args:
          - LARADOCK_PHP_VERSION=${PHP_VERSION}
          - PUID=${PUID}
          - PGID=${PGID}
          - CHROME_DRIVER_VERSION=${WORKSPACE_CHROME_DRIVER_VERSION}
          - TZ=${TIMEZONE}
          - INSTALL_DOCKER_CLIENT=${WORKSPACE_INSTALL_DOCKER_CLIENT}
          - http_proxy
          - https_proxy
          - no_proxy
      volumes:
        - ${APP_CODE_PATH_HOST}:${APP_CODE_PATH_CONTAINER}${APP_CODE_CONTAINER_FLAG}
        - ./php-worker/supervisord.d:/etc/supervisord.d
      extra_hosts:
        - "dockerhost:${DOCKER_HOST_IP}"
      ports:
        - "127.0.0.1:${WORKSPACE_SSH_PORT}:22"
      tty: true
      environment:
        - PHP_IDE_CONFIG=${PHP_IDE_CONFIG}
        - DOCKER_HOST=tcp://docker-in-docker:2376
        - DOCKER_TLS_VERIFY=1
        - DOCKER_TLS_CERTDIR=/certs
        - DOCKER_CERT_PATH=/certs/client
      networks:
        - frontend
        - backend
      links:
        - docker-in-docker

### highcharts ###########################################
    highcharts:
      restart: always
      build:
        context: ./highcharts
        args:
          - PUID=${PUID}
          - PGID=${PGID}
          - TZ=${TIMEZONE}
      tty: true
      ports:
        - "127.0.0.1:${HIGHCHARTS_PORT}:8080"
      networks:
        - backend

### PHP-FPM ##############################################
    php-fpm:
      restart: always
      build:
        context: ./php-fpm
        args:
          - CHANGE_SOURCE=${CHANGE_SOURCE}
          - BASE_IMAGE_TAG_PREFIX=${PHP_FPM_BASE_IMAGE_TAG_PREFIX}
          - LARADOCK_PHP_VERSION=${PHP_VERSION}
          - INSTALL_BZ2=${PHP_FPM_INSTALL_BZ2}
          - INSTALL_ENCHANT=${PHP_FPM_INSTALL_ENCHANT}
          - INSTALL_GMP=${PHP_FPM_INSTALL_GMP}
          - INSTALL_SSH2=${PHP_FPM_INSTALL_SSH2}
          - INSTALL_XSL=${PHP_FPM_INSTALL_XSL}
          - INSTALL_IMAP=${PHP_FPM_INSTALL_IMAP}
          - INSTALL_BCMATH=${PHP_FPM_INSTALL_BCMATH}
          - INSTALL_PHPREDIS=${PHP_FPM_INSTALL_PHPREDIS}
          - INSTALL_MEMCACHED=${PHP_FPM_INSTALL_MEMCACHED}
          - INSTALL_OPCACHE=${PHP_FPM_INSTALL_OPCACHE}
          - INSTALL_EXIF=${PHP_FPM_INSTALL_EXIF}
          - INSTALL_MYSQLI=${PHP_FPM_INSTALL_MYSQLI}
          - INSTALL_INTL=${PHP_FPM_INSTALL_INTL}
          - INSTALL_GHOSTSCRIPT=${PHP_FPM_INSTALL_GHOSTSCRIPT}
          - INSTALL_IMAGE_OPTIMIZERS=${PHP_FPM_INSTALL_IMAGE_OPTIMIZERS}
          - INSTALL_IMAGEMAGICK=${PHP_FPM_INSTALL_IMAGEMAGICK}
          - INSTALL_CALENDAR=${PHP_FPM_INSTALL_CALENDAR}
          - INSTALL_FAKETIME=${PHP_FPM_INSTALL_FAKETIME}
          - INSTALL_APCU=${PHP_FPM_INSTALL_APCU}
          - INSTALL_CACHETOOL=${PHP_FPM_INSTALL_CACHETOOL}
          - INSTALL_YAML=${PHP_FPM_INSTALL_YAML}
          - INSTALL_GETTEXT=${PHP_FPM_INSTALL_GETTEXT}
          - INSTALL_ADDITIONAL_LOCALES=${PHP_FPM_INSTALL_ADDITIONAL_LOCALES}
          - INSTALL_MYSQL_CLIENT=${PHP_FPM_INSTALL_MYSQL_CLIENT}
          - INSTALL_PING=${PHP_FPM_INSTALL_PING}
          - INSTALL_SSHPASS=${PHP_FPM_INSTALL_SSHPASS}
          - INSTALL_MAILPARSE=${PHP_FPM_INSTALL_MAILPARSE}
          - INSTALL_PCNTL=${PHP_FPM_INSTALL_PCNTL}
          - ADDITIONAL_LOCALES=${PHP_FPM_ADDITIONAL_LOCALES}
          - INSTALL_WKHTMLTOPDF=${PHP_FPM_INSTALL_WKHTMLTOPDF}
          - DOWNGRADE_OPENSSL_TLS_AND_SECLEVEL=${PHP_DOWNGRADE_OPENSSL_TLS_AND_SECLEVEL}
          - PUID=${PUID}
          - PGID=${PGID}
          - TZ=${TIMEZONE}
          - IMAGEMAGICK_VERSION=${PHP_FPM_IMAGEMAGICK_VERSION}
          - LOCALE=${PHP_FPM_DEFAULT_LOCALE}
          - INSTALL_DOCKER_CLIENT=${PHP_FPM_INSTALL_DOCKER_CLIENT}
          - http_proxy
          - https_proxy
          - no_proxy
      volumes:
        - ./php-fpm/php${PHP_VERSION}.ini:/usr/local/etc/php/php.ini
        - ${APP_CODE_PATH_HOST}:${APP_CODE_PATH_CONTAINER}${APP_CODE_CONTAINER_FLAG}
      ports:
        - "127.0.0.1:${PHP_FPM_XDEBUG_PORT}:9003"
      expose:
        - "9000"
      extra_hosts:
        - "dockerhost:${DOCKER_HOST_IP}"
      environment:
        - PHP_IDE_CONFIG=${PHP_IDE_CONFIG}
        - DOCKER_HOST=tcp://docker-in-docker:2376
        - DOCKER_TLS_VERIFY=1
        - DOCKER_TLS_CERTDIR=/certs
        - DOCKER_CERT_PATH=/certs/client
        - FAKETIME=${PHP_FPM_FAKETIME}
      depends_on:
        - workspace
      networks:
        - backend
      links:
        - docker-in-docker

### PHP Worker ############################################
    php-worker:
      restart: always
      build:
        context: ./php-worker
        args:
          - CHANGE_SOURCE=${CHANGE_SOURCE}
          - LARADOCK_PHP_VERSION=${PHP_VERSION}
          - INSTALL_BZ2=${PHP_WORKER_INSTALL_BZ2}
          - INSTALL_GD=${PHP_WORKER_INSTALL_GD}
          - INSTALL_IMAGEMAGICK=${PHP_WORKER_INSTALL_IMAGEMAGICK}
          - INSTALL_GMP=${PHP_WORKER_INSTALL_GMP}
          - INSTALL_BCMATH=${PHP_WORKER_INSTALL_BCMATH}
          - INSTALL_MEMCACHED=${PHP_WORKER_INSTALL_MEMCACHED}
          - INSTALL_ZIP_ARCHIVE=${PHP_WORKER_INSTALL_ZIP_ARCHIVE}
          - INSTALL_MYSQL_CLIENT=${PHP_WORKER_INSTALL_MYSQL_CLIENT}
          - INSTALL_AMQP=${PHP_WORKER_INSTALL_AMQP}
          - INSTALL_GHOSTSCRIPT=${PHP_WORKER_INSTALL_GHOSTSCRIPT}
          - INSTALL_REDIS=${PHP_WORKER_INSTALL_REDIS}
          - PUID=${PUID}
          - PGID=${PGID}
          - TZ=${TIMEZONE}
          - IMAGEMAGICK_VERSION=${PHP_WORKER_IMAGEMAGICK_VERSION}
      volumes:
        - ${APP_CODE_PATH_HOST}:${APP_CODE_PATH_CONTAINER}${APP_CODE_CONTAINER_FLAG}
        - ./php-worker/supervisord.d:/etc/supervisord.d
      depends_on:
        - workspace
      extra_hosts:
        - "dockerhost:${DOCKER_HOST_IP}"
      networks:
        - backend

### NGINX Server #########################################
    nginx:
      restart: always
      build:
        context: ./nginx
        args:
          - CHANGE_SOURCE=${CHANGE_SOURCE}
          - PHP_UPSTREAM_CONTAINER=${NGINX_PHP_UPSTREAM_CONTAINER}
          - PHP_UPSTREAM_PORT=${NGINX_PHP_UPSTREAM_PORT}
          - PUID=${PUID}
          - PGID=${PGID}
          - TZ=${TIMEZONE}
          - http_proxy
          - https_proxy
          - no_proxy
      volumes:
        - ${APP_CODE_PATH_HOST}:${APP_CODE_PATH_CONTAINER}${APP_CODE_CONTAINER_FLAG}
        - ${NGINX_HOST_LOG_PATH}:/var/log/nginx
        - ${NGINX_SITES_PATH}:/etc/nginx/sites-available
        - ${NGINX_SSL_PATH}:/etc/nginx/ssl
      ports:
        - "${NGINX_HOST_HTTP_PORT}:80"
        - "${NGINX_HOST_HTTPS_PORT}:443"
      depends_on:
        - php-fpm
      networks:
        - frontend
        - backend
      labels:
        - traefik.port=80
        - traefik.backend=nginx
        - traefik.frontend.rule=Host:opensem.docker.localhost
        - traefik.http.routers.nginx.rule=Host(`opensem².docker.localhost`)

### MySQL ################################################
    mysql:
      restart: always
      build:
        context: ./mysql
        args:
          - MYSQL_VERSION=${MYSQL_VERSION}
      environment:
        - MYSQL_DATABASE=${MYSQL_DATABASE}
        - MYSQL_USER=${MYSQL_USER}
        - MYSQL_PASSWORD=${MYSQL_PASSWORD}
        - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
        - TZ=${TIMEZONE}
      volumes:
        - ${DATA_PATH_HOST}/mysql:/var/lib/mysql
        - ${MYSQL_ENTRYPOINT_INITDB}:/docker-entrypoint-initdb.d
      ports:
        - "127.0.0.1:${MYSQL_PORT}:3306"
      networks:
        - backend

### MySQL ################################################
    mysql-slave1:
      build:
        context: ./mysql-slave1
        args:
          - MYSQL_VERSION=${MYSQL_VERSION}
      environment:
        - MYSQL_DATABASE=${MYSQL_DATABASE}
        - MYSQL_USER=${MYSQL_USER}
        - MYSQL_PASSWORD=${MYSQL_PASSWORD}
        - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
        - TZ=${TIMEZONE}
      volumes:
        - ${DATA_PATH_HOST}/mysql-slave1:/var/lib/mysql
        - ${MYSQL_ENTRYPOINT_INITDB}:/docker-entrypoint-initdb.d
      ports:
        - "127.0.0.1:3307:3306"
      networks:
        - backend

### Redis ################################################
    redis:
      restart: always
      build: ./redis
      volumes:
        - ${DATA_PATH_HOST}/redis:/data
      command: redis-server --requirepass ${REDIS_PASSWORD}
      ports:
        - "127.0.0.1:${REDIS_PORT}:6379"
      networks:
        - backend

### SMTP ################################################
    smtp:
      restart: always
      build: ./smtp
      ports:
        - "${SMTP_PORT}:25"
      networks:
        - frontend
        - backend
      tty: true

### phpMyAdmin ###########################################
    phpmyadmin:
      build: ./phpmyadmin
      environment:
        - PMA_ARBITRARY=1
        - MYSQL_USER=${PMA_USER}
        - MYSQL_PASSWORD=${PMA_PASSWORD}
        - MYSQL_ROOT_PASSWORD=${PMA_ROOT_PASSWORD}
        - MAX_EXECUTION_TIME=${PMA_MAX_EXECUTION_TIME}
        - MEMORY_LIMIT=${PMA_MEMORY_LIMIT}
        - UPLOAD_LIMIT=${PMA_UPLOAD_LIMIT}
      ports:
        - "127.0.0.1:${PMA_PORT}:80"
      depends_on:
        - "${PMA_DB_ENGINE}"
      networks:
        - frontend
        - backend

### Certbot #########################################
    certbot:
      build:
        context: ./certbot
      volumes:
        - ./data/certbot/certs/:/var/certs
        - ./certbot/letsencrypt/:/var/www/letsencrypt
      environment:
        - CN="digital.opensem.ovh"
        - EMAIL="ludo@huma.net"
      networks:
        - frontend

### Docker-in-Docker ################################################
    docker-in-docker:
      image: docker:19.03-dind
      environment:
        DOCKER_TLS_SAN: DNS:docker-in-docker
      privileged: true
      volumes:
        - ${APP_CODE_PATH_HOST}:${APP_CODE_PATH_CONTAINER}
        - docker-in-docker:/certs/client
      expose:
        - 2375
      networks:
        - backend

### Soketi ###########################################
    soketi:
      restart: unless-stopped
      image: 'quay.io/soketi/soketi:1.0-16-debian'
      ports:
        - '${SOKETI_PORT:-6001}:6001'
        - '${SOKETI_METRICS_SERVER_PORT:-9601}:9601'
      environment:
        - SOKETI_DEBUG=1
        - SOKETI_DEFAULT_APP_ID=${PUSHER_APP_ID}
        - SOKETI_DEFAULT_APP_KEY=${PUSHER_APP_KEY}
        - SOKETI_DEFAULT_APP_SECRET=${PUSHER_APP_SECRET}
        - SOKETI_DEFAULT_APP_ENABLE_CLIENT_MESSAGES=true
        - DEFAULT_APP_USER_AUTHENTICATION=true
      expose:
        - "6001"
      extra_hosts:
        - "dockerhost:${DOCKER_HOST_IP}"
      networks:
        - backend
        - frontend
      links:
        - docker-in-docker

### Laravel Horizon ############################################
    laravel-horizon:
      build:
        context: ./laravel-horizon
        args:
          - CHANGE_SOURCE=${CHANGE_SOURCE}
          - LARADOCK_PHP_VERSION=${PHP_VERSION}
          - INSTALL_BZ2=${LARAVEL_HORIZON_INSTALL_BZ2}
          - INSTALL_GD=${LARAVEL_HORIZON_INSTALL_GD}
          - INSTALL_GMP=${LARAVEL_HORIZON_INSTALL_GMP}
          - INSTALL_GNUPG=${LARAVEL_HORIZON_INSTALL_GNUPG}
          - INSTALL_LDAP=${LARAVEL_HORIZON_INSTALL_LDAP}
          - INSTALL_IMAGEMAGICK=${LARAVEL_HORIZON_INSTALL_IMAGEMAGICK}
          - INSTALL_PGSQL=${PHP_FPM_INSTALL_PGSQL}
          - INSTALL_ZIP_ARCHIVE=${LARAVEL_HORIZON_INSTALL_ZIP_ARCHIVE}
          - INSTALL_BCMATH=${PHP_FPM_INSTALL_BCMATH}
          - INSTALL_MEMCACHED=${PHP_FPM_INSTALL_MEMCACHED}
          - INSTALL_SOCKETS=${LARAVEL_HORIZON_INSTALL_SOCKETS}
          - INSTALL_YAML=${LARAVEL_HORIZON_INSTALL_YAML}
          - INSTALL_CASSANDRA=${LARAVEL_HORIZON_INSTALL_CASSANDRA}
          - INSTALL_PHPREDIS=${LARAVEL_HORIZON_INSTALL_PHPREDIS}
          - INSTALL_MONGO=${LARAVEL_HORIZON_INSTALL_MONGO}
          - INSTALL_FFMPEG=${LARAVEL_HORIZON_INSTALL_FFMPEG}
          - INSTALL_AUDIOWAVEFORM=${LARAVEL_HORIZON_INSTALL_AUDIOWAVEFORM}
          - PUID=${LARAVEL_HORIZON_PUID}
          - PGID=${LARAVEL_HORIZON_PGID}
          - IMAGEMAGICK_VERSION=${LARAVEL_HORIZON_IMAGEMAGICK_VERSION}
      volumes:
        - ${APP_CODE_PATH_HOST}:${APP_CODE_PATH_CONTAINER}
        - ./laravel-horizon/supervisord.d:/etc/supervisord.d
      depends_on:
        - workspace
      extra_hosts:
        - "dockerhost:${DOCKER_HOST_IP}"
      networks:
        - backend

### Proxy ###########################################
    proxy:
      restart: always
      build:
        context: ./proxy
        args:
          - PUID=${PUID}
          - PGID=${PGID}
          - TZ=${TIMEZONE}
          - http_proxy
          - https_proxy
          - no_proxy
      volumes:
        - ${APP_CODE_PATH_HOST}:${APP_CODE_PATH_CONTAINER}${APP_CODE_CONTAINER_FLAG}
        - ${PROXY_HOST_LOG_PATH}:/var/log/nginx
        - ${PROXY_SITES_PATH}:/etc/nginx/sites-available
        - ${PROXY_SSL_PATH}:/etc/nginx/ssl
      ports:
        - "${NGINX_HOST_HTTP_PORT}:80"
        - "${NGINX_HOST_HTTPS_PORT}:443"
      networks:
        - frontend
        - backend
