# version: '3'

# services:
#   portainer:
#     image: portainer/portainer-ce:latest
#     container_name: portainer
#     networks:
#       - traefik
#     restart: unless-stopped
#     security_opt:
#       - no-new-privileges:true
#     volumes:
#       - /etc/localtime:/etc/localtime:ro
#       - /var/run/docker.sock:/var/run/docker.sock:ro
#       - /srv/portainer/portainer-data:/data
#     ports:
#       - 9000:9000
#     labels:
      # - "traefik.enable=true"
      # - "traefik.http.routers.portainer.entrypoints=websecure"
      # - "traefik.http.routers.portainer.rule=Host(`portainer.$DOMAIN_NAME`)"
      # - "traefik.http.routers.portainer.tls.certresolver=letsencr"
      # - "traefik.http.services.portainer.loadbalancer.server.port=9000"
#       - "traefik.http.routers.portainer.middlewares=portainer_auth"
#       - "traefik.http.middlewares.portainer_auth.basicauth.users=admin:$$2a$$12$$MkhjqyuriyoE5.Mhz14ZXOkfjE7wUEDb46e9argix38VHZ5aAuGQC"

version: "3.7"

services:
  # rocketchat:
  #   image: registry.rocket.chat/rocketchat/rocket.chat:latest
  #   restart: unless-stopped
  #   volumes:
    #  - /srv/rocket/uploads:/app/uploads
  #   environment:
  #     MONGO_URL: "mongodb://mongodb:27017/rocketchat?replicaSet=rs0"
  #     MONGO_OPLOG_URL: "mongodb://mongodb:27017/local?replicaSet=rs0"
  #     ROOT_URL: "https://rocket.knast.cc"
  #     # PORT: 3000
  #   labels:
      # - "traefik.enable=true"
      # - "traefik.http.routers.rocket.entrypoints=websecure"
      # - "traefik.http.routers.rocket.rule=Host(`rocket.$DOMAIN_NAME`)"
      # - "traefik.http.routers.rocket.tls.certresolver=letsencr"
      # - "traefik.http.services.rocket.loadbalancer.server.port=3000"
  #   depends_on:
  #     - mongodb



  rocketchat:
    image: ${ROCKETCHAT_IMAGE_TAG}
    volumes:
      - /srv/rocket/uploads:/app/uploads
    environment:
      PORT: 3000
      ROOT_URL: ${ROCKETCHAT_URL}
      MONGO_URL: mongodb://mongodb:27017/rocketchat?replicaSet=rs0
      MONGO_OPLOG_URL: mongodb://mongodb:27017/local?replicaSet=rs0
      DEPLOY_METHOD: docker
      Accounts_UseDNSDomainCheck: 'false'
    networks:
      - traefik
    healthcheck:
      test: >
         /usr/local/bin/node -e '
         const http = require("http");
         const options = {
           host: "localhost",
           port: 3000,
           path: "/api/info",
           timeout: 2000
         };
         const healthCheck = http.request(options, (res) => {
           console.log(`HEALTHCHECK STATUS: $${res.statusCode}`);
           if (res.statusCode == 200) {
             process.exit(0);
           } else {
             process.exit(1);
           }
         });
         healthCheck.on("error", function (err) {
           console.error("ERROR");
           process.exit(1);
         });
         healthCheck.end();'
      interval: 10s
      timeout: 5s
      retries: 3
      start_period: 120s
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.rocket.entrypoints=websecure"
      - "traefik.http.routers.rocket.rule=Host(`rocket.$DOMAIN_NAME`)"
      - "traefik.http.routers.rocket.tls.certresolver=letsencr"
      - "traefik.http.services.rocket.loadbalancer.server.port=3000"

      # - "traefik.enable=true"
      # - "traefik.http.routers.rocketchat.rule=Host(`${ROCKETCHAT_HOSTNAME}`)"
      # - "traefik.http.routers.rocketchat.service=rocketchat"
      # - "traefik.http.routers.rocketchat.entrypoints=websecure"
      # - "traefik.http.services.rocketchat.loadbalancer.server.port=3000"
      # - "traefik.http.routers.rocketchat.tls=true"
      # - "traefik.http.routers.rocketchat.tls.certresolver=letsencrypt"
      # - "traefik.http.services.rocketchat.loadbalancer.passhostheader=true"
      # - "traefik.http.routers.rocketchat.middlewares=compresstraefik"
      # - "traefik.http.middlewares.compresstraefik.compress=true"
      # - "traefik.docker.network=traefik-network"
    restart: unless-stopped
    depends_on:
      mongodb:
        condition: service_healthy

  mongodb:
    image: docker.io/bitnami/mongodb:8.0.4
    restart: unless-stopped
    volumes:
      - rocket-data:/bitnami/mongodb
    networks:
      - traefik
    # volumes:
    #   - type: bind
    #     source: /srv/rocket/db
    #     target: /bitnami/mongodb
    healthcheck:
      test: echo 'db.runCommand("ping").ok' | mongosh localhost:27017/rocketchat --quiet
      interval: 10s
      timeout: 10s
      retries: 3
      start_period: 20s
    environment:
      MONGODB_REPLICA_SET_MODE: primary
      MONGODB_REPLICA_SET_NAME: rs0
      MONGODB_PORT_NUMBER: 27017
      MONGODB_INITIAL_PRIMARY_HOST: mongodb
      MONGODB_INITIAL_PRIMARY_PORT_NUMBER: 7017
      MONGODB_ADVERTISED_HOSTNAME: mongodb
      MONGODB_ENABLE_JOURNAL: true
      ALLOW_EMPTY_PASSWORD: yes

volumes:
  rocket-data:
    driver: local
    driver_opts:
      o: bind
      type: none
      device: /srv/rocket/db

networks:
  traefik:
    external: true
