# To execute this docker-compose yml file use docker-compose -f <file_name> up
# Add the "-d" flag at the end for detached execution
version: '3.8'
services:
  openapi:
    container_name: ${APP_NAME}_openapi
    image: swaggerapi/swagger-ui
    ports:
      - ${OPENAPI_PORT}:8080
    environment:
      URL: "http://localhost:8080"

  rabbitmq:
    container_name: ${APP_NAME}_rabbitmq
    image: rabbitmq:management-alpine
    ports:
      - ${RABBITMQ_PORT}:5672
      - ${RABBITMQ_MANAGEMENT_PORT}:15672

  redis:
    container_name: ${APP_NAME}_redis
    image: redis:6-alpine
    ports:
      - ${REDIS_PORT}:6379
    volumes:
      - redis-data:/var/lib/redis:rw

  app:
    container_name: ${APP_NAME}_app
    build:
      context: .
      dockerfile: docker/app/Dockerfile
    ports:
      - ${APP_PORT}:8080
    environment:
      DB_HOST: ${DB_HOST}
      SEND_GRID_KEY: ${SEND_GRID_KEY}
      REDIS_CONNECTION: ${REDIS_CONNECTION}
      RABBITMQ_CONNECTION: ${RABBITMQ_CONNECTION}
      ELASTICSEARCH_CONNECTION: ${ELASTICSEARCH_CONNECTION}
      JWT_SECRET: ${JWT_SECRET}
      JWT_ISSUER: ${JWT_ISSUER}
      JWT_VALIDITY: ${JWT_VALIDITY}
    depends_on:
      - elasticsearch
      - db
      - redis
      - rabbitmq

  elasticsearch:
    container_name: ${APP_NAME}_elasticsearch
    image: elasticsearch:6.7.1
    ports:
      - ${ELASTIC_REST}:9200
      - ${ELASTIC_NODES}:9300
    healthcheck:
      test: ["CMD", "curl", "-f", "http://elasticsearch:9200"]
      interval: 3s
      timeout: 2s
      retries: 20

  db:
    container_name: ${APP_NAME}_postgresql
    build:
      context: docker/postgresql
    ports:
      - ${POSTGRESQL_PORT}:5432
    environment:
      POSTGRES_PASSWORD: ${DB_NAME}
      POSTGRES_USER: ${DB_USER}
      POSTGRES_DB: ${DB_PWD}
    volumes:
      - ./var/log/postgresql:/var/log/postgresql:rw
      - db-data:/var/lib/postgresql/data:rw
    depends_on:
      - elasticsearch
    healthcheck:
      test: [ "CMD", "pg_isready", "-q", "-d", "${DB_NAME}", "-U", "${DB_USER}" ]
      interval: 3s
      timeout: 2s
      retries: 20

volumes:
  db-data:
  redis-data: