##
## -------------------------
## |  D E V I L S T A C K  |
## -------------------------
##
## Local LAMP/LEMP stack
##
##
## ${VARIABLE:-default} will evaluate to default if VARIABLE is unset or empty in the environment.
## ${VARIABLE-default}  will evaluate to default only if VARIABLE is unset in the environment.
##
##
## -- DO NOT EDIT THIS FILE --
##
## Edit '.env' for configuration.
##
## If '.env' does not exist, copy 'env-example' to '.env'
##   $ cp env-example .env
##


version: '2.1'


################################################################################
# SERVICES
################################################################################
services:

  # ------------------------------------------------------------
  # Bind (DNS Server)
  # ------------------------------------------------------------
  bind:
    image: cytopia/bind:0.11
    restart: always
    ports:
      # [local-machine:]local-port:docker-port
      - "${LOCAL_LISTEN_ADDR}${HOST_PORT_BIND:-1053}:53"
      - "${LOCAL_LISTEN_ADDR}${HOST_PORT_BIND:-1053}:53/udp"

    environment:
      ##
      ## Debug?
      ##
      - DEBUG_COMPOSE_ENTRYPOINT

      ##
      ## Bind settings
      ##
      - WILDCARD_DOMAIN=${TLD_SUFFIX:-loc}
      - WILDCARD_ADDRESS=172.16.238.11
      - DNS_FORWARDER=${BIND_DNS_RESOLVER:-8.8.8.8,8.8.4.4}

    dns:
      - 127.0.0.1

    networks:
      app_net:
        ipv4_address: 172.16.238.100


  # ------------------------------------------------------------
  # PHP / HHVM
  # ------------------------------------------------------------
  php:
    image: cytopia/${PHP_SERVER:-php-fpm-7.0}:latest
    restart: always

    ##
    ## All .env variables
    ##
    ## Source all variables defined in .env
    ## This also makes any custom variable available in each PHP/HHVM container
    ##
    env_file:
      - ./.env

    environment:
      ##
      ## Debug?
      ##
      - DEBUG_COMPOSE_ENTRYPOINT

      ##
      ## UserID and GroupID
      ##
      - NEW_UID
      - NEW_GID

      ##
      ## Adjust timezone
      ##
      - TIMEZONE

      ##
      ## PHP Xdebug
      ##
      - PHP_XDEBUG_ENABLE
      - PHP_XDEBUG_REMOTE_PORT
      - PHP_XDEBUG_REMOTE_HOST

      ##
      ## Mail-catching
      ##
      - ENABLE_MAIL=1

      ##
      ## Enable 127.0.0.1 Port-forwarding
      ##
      - FORWARD_PORTS_TO_LOCALHOST=3306:mysql:3306,5432:pgsql:5432,6379:redis:6379,11211:memcd:11211,27017:mongo:27017

      ##
      ## MySQL Backups
      ##
      - MYSQL_BACKUP_USER=root
      - MYSQL_BACKUP_PASS=${MYSQL_ROOT_PASSWORD}
      - MYSQL_BACKUP_HOST=mysql

    networks:
      app_net:
        ipv4_address: 172.16.238.10

    dns:
      - 172.16.238.100
      - 8.8.8.8
      - 8.8.4.4

    volumes:
      # ---- Format: ----
      # HOST-DIRECTORY : DOCKER-DIRECTORY

      # Mount custom intranet
      - ${DEVILBOX_PATH}/.devilbox/www:/var/www/default:ro

      # Mount custom mass virtual hosting
      - ${HOST_PATH_HTTPD_DATADIR}:/shared/httpd

      # Mount logs
      - ${DEVILBOX_PATH}/log/${PHP_SERVER}:/var/log/php

      # Mount Mail directory
      #- ${DEVILBOX_PATH}/run/mail:/var/mail

      # Mount DB Backup directory
      - ${DEVILBOX_PATH}/backups:/shared/backups

      # Mount devilbox user-defined *.ini files in order
      # to overwrite the default PHP configuration
      - ${DEVILBOX_PATH}/cfg/${PHP_SERVER}:/etc/php-custom.d:ro

      # Mount devilbox user-defined *.so files in order
      # to load custom PHP modules
      - ${DEVILBOX_PATH}/mod/${PHP_SERVER}:/usr/lib64/php/custom-modules:ro

      # Mount devilbox user-defined bash config
      - ${DEVILBOX_PATH}/bash:/etc/bashrc-devilbox.d

    depends_on:
      - bind


  # ------------------------------------------------------------
  # Web Server
  # ------------------------------------------------------------
  httpd:
    image: devilbox/${HTTPD_SERVER:-nginx-stable}:0.12
    restart: always

    environment:

      ##
      ## Debug?
      ##
      - DEBUG_ENTRYPOINT=${DEBUG_COMPOSE_ENTRYPOINT}
      - DEBUG_RUNTIME=${DEBUG_COMPOSE_ENTRYPOINT}

      ##
      ## Adjust timezone
      ##
      - TIMEZONE

      ##
      ## UserID and GroupID
      ##
      - NEW_UID
      - NEW_GID

      ##
      ## Disable default vhost?
      ##
      - MAIN_VHOST_DISABLE=${DEVILBOX_UI_DISABLE}
      - MAIN_VHOST_STATUS_ENABLE=1
      - MAIN_VHOST_STATUS_ALIAS=/devilbox-httpd-status

      ##
      ## Enable Mass Vhosts
      ##
      - MASS_VHOST_ENABLE=1
      - MASS_VHOST_TLD=.${TLD_SUFFIX}
      - MASS_VHOST_DOCROOT=${HTTPD_DOCROOT_DIR}
      - MASS_VHOST_TPL=${HTTPD_TEMPLATE_DIR}

      ##
      ## PHP-FPM Remote Server
      ##
      - PHP_FPM_ENABLE=1
      - PHP_FPM_SERVER_ADDR=php
      - PHP_FPM_SERVER_PORT=9000

    ports:
      # ---- Format: ----
      # [HOST-ADDR : ] HOST-PORT : DOCKER-PORT
      - "${LOCAL_LISTEN_ADDR}${HOST_PORT_HTTPD}:80"

    networks:
      app_net:
        ipv4_address: 172.16.238.11

    volumes:
      # ---- Format: ----
      # HOST-DIRECTORY : DOCKER-DIRECTORY

      # Mount custom intranet
      - ${DEVILBOX_PATH}/.devilbox/www:/var/www/default:ro

      # Mount custom mass virtual hosting
      - ${HOST_PATH_HTTPD_DATADIR}:/shared/httpd

      # Mount custom web server config directory
      - ${DEVILBOX_PATH}/cfg/${HTTPD_SERVER}:/etc/httpd-custom.d

      # Mount logs
      - ${DEVILBOX_PATH}/log/${HTTPD_SERVER}:/var/log/${HTTPD_SERVER}

    depends_on:
      - bind
      - php


  # ------------------------------------------------------------
  # MySQL Database
  # ------------------------------------------------------------
  mysql:
    image: cytopia/${MYSQL_SERVER:-mariadb-10.1}:latest

    environment:

      ##
      ## Debug?
      ##
      - DEBUG_COMPOSE_ENTRYPOINT

      ##
      ## Adjust timezone
      ##
      - TIMEZONE

      ##
      ## Adjust Root password
      ##
      - MYSQL_ROOT_PASSWORD

      ##
      ## Socket directory Path
      ##
      - MYSQL_SOCKET_DIR=/tmp/mysql

      ##
      ## Runtime settings
      ##
      - MYSQL_GENERAL_LOG=${MYSQL_GENERAL_LOG}

    ports:
      # [local-machine:]local-port:docker-port
      - "${LOCAL_LISTEN_ADDR}${HOST_PORT_MYSQL}:3306"

    networks:
      app_net:
        ipv4_address: 172.16.238.12

    volumes:
      # ---- Format: ----
      # HOST-DIRECTORY : DOCKER-DIRECTORY

      # Mount logs
      - ${DEVILBOX_PATH}/log/${MYSQL_SERVER}:/var/log/mysql

      # Mount devilbox default overwrites
      - ${DEVILBOX_PATH}/.devilbox/etc/${MYSQL_SERVER}:/etc/mysql/conf.d:ro

      # Mount devilbox user-defined cnf files in order
      # to overwrite the MySQL server configuration
      - ${DEVILBOX_PATH}/cfg/${MYSQL_SERVER}:/etc/mysql/docker-default.d:ro

      # Mount MySQL Data directory
      - ${HOST_PATH_MYSQL_DATADIR}/${MYSQL_SERVER}:/var/lib/mysql

    depends_on:
      - bind
      - php
      - httpd


  # ------------------------------------------------------------
  # PostgreSQL
  # ------------------------------------------------------------
  pgsql:
    image: postgres:${PGSQL_SERVER:-9.6}

    environment:

      - POSTGRES_USER=${PGSQL_ROOT_USER}
      - POSTGRES_PASSWORD=${PGSQL_ROOT_PASSWORD}
      - PGDATA=/var/lib/postgresql/data/pgdata

    ports:
      # [local-machine:]local-port:docker-port
      - "${LOCAL_LISTEN_ADDR}${HOST_PORT_PGSQL}:5432"

    networks:
      app_net:
        ipv4_address: 172.16.238.13

    volumes:
      # ---- Format: ----
      # HOST-DIRECTORY : DOCKER-DIRECTORY

      # Mount logs
      - ${DEVILBOX_PATH}/log/pgsql-${PGSQL_SERVER}:/var/log/postgresql

      # Mount PostgreSQL Data directory
      - ${HOST_PATH_PGSQL_DATADIR}/${PGSQL_SERVER}:/var/lib/postgresql/data/pgdata

    depends_on:
      - bind
      - php
      - httpd


  # ------------------------------------------------------------
  # Redis
  # ------------------------------------------------------------
  redis:
    image: redis:${REDIS_SERVER:-3.2}

    ports:
      # [local-machine:]local-port:docker-port
      - "${LOCAL_LISTEN_ADDR}${HOST_PORT_REDIS}:6379"

    networks:
      app_net:
        ipv4_address: 172.16.238.14

    volumes:
      # ---- Format: ----
      # HOST-DIRECTORY : DOCKER-DIRECTORY

      # Mount logs
      - ${DEVILBOX_PATH}/log/redis-${REDIS_SERVER}:/var/log/redis

    depends_on:
      - bind
      - php
      - httpd


  # ------------------------------------------------------------
  # Memcached
  # ------------------------------------------------------------
  memcd:
    image: memcached:${MEMCD_SERVER:-latest}

    ports:
      # [local-machine:]local-port:docker-port
      - "${LOCAL_LISTEN_ADDR}${HOST_PORT_MEMCD}:11211"

    networks:
      app_net:
        ipv4_address: 172.16.238.15

    volumes:
      # ---- Format: ----
      # HOST-DIRECTORY : DOCKER-DIRECTORY

      # Mount logs
      - ${DEVILBOX_PATH}/log/memcd-${MEMCD_SERVER}:/var/log/memcd

    depends_on:
      - bind
      - php
      - httpd


  # ------------------------------------------------------------
  # MongoDB
  # ------------------------------------------------------------
  mongo:
    image: mongo:${MONGO_SERVER:-latest}

    ports:
      # [local-machine:]local-port:docker-port
      - "${LOCAL_LISTEN_ADDR}${HOST_PORT_MONGO}:27017"

    networks:
      app_net:
        ipv4_address: 172.16.238.16

    volumes:
      # ---- Format: ----
      # HOST-DIRECTORY : DOCKER-DIRECTORY

      # Mount MongoDB Data directory
      - ${HOST_PATH_MONGO_DATADIR}/${MONGO_SERVER}:/data/db

    depends_on:
      - bind
      - php
      - httpd


################################################################################
# NETWORK
################################################################################
networks:
  app_net:
    driver: bridge
    driver_opts:
      com.docker.network.enable_ipv6: "false"
    ipam:
      driver: default
      config:
        - subnet: 172.16.238.0/24
          gateway: 172.16.238.1