version: "3.6"

services:
  # Prepares the bind-mounted folders for the Tributech Agent, which runs as the
  # non-root user 1654 and ships without the tooling to change ownership itself.
  # This replaces a manual `sudo chown -R 1654:1654 enrollment volumes` on the host.
  init-permissions:
    image: busybox:1.36
    user: "0:0"
    command: sh -c "chown -R 1654:1654 /mnt/enrollment /mnt/volumes"
    volumes:
      - ./enrollment:/mnt/enrollment
      - ./volumes:/mnt/volumes

  source-mqtt:
    image: ${DOCKER_REGISTRY-tributech.azurecr.io/}tributech-source-mqtt:${SOURCE_TAG:-5.3.0}
    depends_on:
      - mosquitto-server
      - tributech-agent
    environment:
      - MqttOptions__MQTTHost=mosquitto-server
      - Logging__LogLevel__Default=Information
    networks:
      - mqtt-net
    restart: unless-stopped
    logging:
      driver: "json-file"
      options:
        max-size: "2m"
        max-file: "5"

  tributech-agent:
    image: ${DOCKER_REGISTRY-tributech.azurecr.io/}tributech-agent:${AGENT_TAG:-5.5.0}
    depends_on:
      mosquitto-server:
        condition: service_started
      init-permissions:
        condition: service_completed_successfully
    environment:
      - Logging__LogLevel__Default=Information
      - Logging__Console__FormatterName=simple
      - MqttOptions__MQTTHost=mosquitto-server
      - EdgeDeviceOptions__NodeUrl=${NODE_URL:?"The Tributech Node Url is required"}
    networks:
      - mqtt-net
    ports:
      - "5001:8080" # enable access to agent mqtt-API (e.g. for configuration with Agent-Companion)
    volumes:
      - ./volumes:/app/data # volume mapping for permanent storage of keys and datatwin file
      - ./enrollment:/app/enrollment # local enrollment folder mounted to /app/enrollment
    restart: unless-stopped
    logging:
      driver: "json-file"
      options:
        max-size: "2m"
        max-file: "5"

  mosquitto-server:
    image: eclipse-mosquitto:${MQTT_TAG:-1.6}
    networks:
      - mqtt-net
    ports:
     - '1883:1883' # MQTT
    # - "127.0.0.1:9001:9001" # web-socket
    restart: unless-stopped

networks:
  mqtt-net:
