version: '3'

services:
  base: &base
    image: chatwoot/chatwoot:latest
    env_file: .env ## Change this file for customized env variables
    networks:
      - chatwoot-network
    volumes:
      - datachat:/app/storage

  rails:
    <<: *base
    depends_on:
      - postgres
      - redis
    environment:
      - NODE_ENV=production
      - RAILS_ENV=production
      - INSTALLATION_ENV=docker
    entrypoint: docker/entrypoints/rails.sh
    command: ['bundle', 'exec', 'rails', 's', '-p', '3000', '-b', '0.0.0.0']
    networks:
      - traefik-proxy
      - chatwoot-network
    labels:
      - traefik.enable=true
      - traefik.http.routers.chatwoot.rule=Host(`${URL_CHATWOOT}`)
      - traefik.http.routers.chatwoot.entrypoints=websecure
      - traefik.http.routers.chatwoot.tls=true
      - traefik.http.routers.chatwoot.tls.certresolver=lets-encrypt
      - traefik.http.routers.chatwoot.service=chatwoot
      - traefik.http.services.chatwoot.loadbalancer.server.port=3000
      - traefik.docker.network=${NETWORK_TRAEFIK}

  sidekiq:
    <<: *base
    depends_on:
      - postgres
      - redis
    networks:
      - chatwoot-network
    environment:
      - NODE_ENV=production
      - RAILS_ENV=production
      - INSTALLATION_ENV=docker
    command: ['bundle', 'exec', 'sidekiq', '-C', 'config/sidekiq.yml']

  postgres:
    image: postgres:12
    container_name: postgreschatwoot
    restart: always
    volumes:
      - postgreschat:/var/lib/postgresql/data
    networks:
      - chatwoot-network
    environment:
      - PGDATA=/var/lib/postgresql/data
      - POSTGRES_DB=chatwoot
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=wovktGH2EtM6i477oEBQ

  redis:
    image: redis:alpine
    container_name: redischatwoot
    restart: always
    command: ["sh", "-c", "redis-server --requirepass \"$REDIS_PASSWORD\""]
    env_file: .env
    volumes:
      - redischat:/data
    networks:
      - chatwoot-network

networks:
  traefik-proxy:
    external:
      name: ${NETWORK_TRAEFIK}
  chatwoot-network:
    internal: true
      

volumes:
  postgreschat:
    driver: local
    driver_opts:
      type: none
      device: ${VOLUME_POSTGRES_CHAT}
      o: bind

  redischat:
    driver: local
    driver_opts:
      type: none
      device: ${VOLUME_REDIS_CHAT}
      o: bind

  datachat:
    driver: local
    driver_opts:
      type: none
      device: ${VOLUME_DATA_CHAT}
      o: bind
