# This file defines the production settings. It is overwritten by docker-compose.override.yml,
# which defines the development settings. The override.yml is loaded by default. Therefore it
# is required to explicitly define if you want an production build:
# > docker-compose -f docker-compose.yml up 

version: "3.4"

services:

  ########################################################
  # FRONTEND #############################################
  ########################################################
  frontend:
    # name the image so that it cannot be found in a DockerHub repository, otherwise it will not be built locally from the 'dockerfile' but pulled from there
    image: gradido/frontend:local-production
    build:
      context: ./frontend
      target: production
    networks:
      - external-net
      - internal-net
    ports:
      - 3000:3000
    environment:
      # Envs used in Dockerfile
      # - DOCKER_WORKDIR="/app"
      # - PORT=3000
      # - BUILD_DATE="1970-01-01T00:00:00.00Z"
      # - BUILD_VERSION="0.0.0.0"
      # - BUILD_COMMIT="0000000"
      - NODE_ENV="production"
    # env_file:
      # - ./.env
      # - ./frontend/.env

  ########################################################
  # ADMIN INTERFACE ######################################
  ########################################################
  admin:
    # name the image so that it cannot be found in a DockerHub repository, otherwise it will not be built locally from the 'dockerfile' but pulled from there
    image: gradido/admin:local-production
    build:
      context: ./admin
      target: production
    networks:
      - external-net
      - internal-net
    ports:
      - 8080:8080
    environment:
      # Envs used in Dockerfile
      # - DOCKER_WORKDIR="/app"
      # - PORT=8090
      # - BUILD_DATE="1970-01-01T00:00:00.00Z"
      # - BUILD_VERSION="0.0.0.0"
      # - BUILD_COMMIT="0000000"
      - NODE_ENV="production"
    # env_file:
      # - ./.env
      # - ./admin/.env

  #########################################################
  ## MARIADB ##############################################
  #########################################################
  mariadb:
    build:
      context: ./mariadb
      target: mariadb_server
    environment:
      - MARIADB_ALLOW_EMPTY_PASSWORD=1
      - MARIADB_USER=root
    networks: 
      - internal-net
    ports: 
      - 3306:3306
    volumes: 
      - db_vol:/var/lib/mysql
      
  ########################################################
  # BACKEND ##############################################
  ########################################################
  backend:
    # name the image so that it cannot be found in a DockerHub repository, otherwise it will not be built locally from the 'dockerfile' but pulled from there
    image: gradido/backend:local-production
    build:
      # since we have to include the entities from ./database we cannot define the context as ./backend
      # this might blow build image size to the moon ?!
      context: ./
      dockerfile: ./backend/Dockerfile
      target: production
    networks:
      - internal-net
    ports:
      - 4000:4000
    depends_on:
      - mariadb
    restart: always
    environment:
      # Envs used in Dockerfile
      # - DOCKER_WORKDIR="/app"
      # - PORT=4000
      - BUILD_DATE
      - BUILD_VERSION
      - BUILD_COMMIT
      - NODE_ENV="production"
      - DB_HOST=mariadb
      # Application only envs
    #env_file:
    #  - ./frontend/.env
    volumes:
      # <host_machine_directory>:<container_directory> – mirror bidirectional path in local context with path in Docker container
      - ./logs/backend:/logs/backend

  ########################################################
  # DHT-NODE #############################################
  ########################################################
  dht-node:
    # name the image so that it cannot be found in a DockerHub repository, otherwise it will not be built locally from the 'dockerfile' but pulled from there
    image: gradido/dht-node:local-production
    build:
      # since we have to include the entities from ./database we cannot define the context as ./backend
      # this might blow build image size to the moon ?!
      context: ./
      dockerfile: ./dht-node/Dockerfile
      target: production
    networks:
      - internal-net
      - external-net
    #ports:
    #  - 5000:5000
    depends_on:
      - mariadb
    restart: always
    environment:
      # Envs used in Dockerfile
      # - DOCKER_WORKDIR="/app"
      # - PORT=5000
      - BUILD_DATE
      - BUILD_VERSION
      - BUILD_COMMIT
      - NODE_ENV="production"
      - DB_HOST=mariadb
      # Application only envs
    #env_file:
    #  - ./frontend/.env
    volumes:
      # <host_machine_directory>:<container_directory> – mirror bidirectional path in local context with path in Docker container
      - ./logs/dht-node:/logs/dht-node

  ########################################################
  # DATABASE #############################################
  ########################################################
  database:
    # name the image so that it cannot be found in a DockerHub repository, otherwise it will not be built locally from the 'dockerfile' but pulled from there
    image: gradido/database:local-production_up
    build:
      context: ./database
      target: production_up
    depends_on:
      - mariadb
    networks:
      - internal-net
      - external-net # this is required to fetch the packages
    environment:
      # Envs used in Dockerfile
      # - DOCKER_WORKDIR="/app"
      - BUILD_DATE
      - BUILD_VERSION
      - BUILD_COMMIT
      - NODE_ENV="production"
      - DB_HOST=mariadb
      # Application only envs
    #env_file:
    #  - ./frontend/.env
    
  #########################################################
  ## NGINX ################################################
  #########################################################
  nginx:
    build: 
      context: ./nginx/
    networks:
      - external-net
      - internal-net
    depends_on:
      - frontend
      - backend
      - admin 
    ports:
      - 80:80
    volumes: 
      - ./logs/nginx:/var/log/nginx 
  
networks:
  external-net:
  internal-net:
    internal: true

volumes:
  db_vol: 
  
