name: qbt-bench
services:
  qbittorrent:
    container_name: qbittorrent
    deploy:
      resources:
        limits:
          memory: "1073741824"
    entrypoint:
      - /bin/sh
      - -c
      - |
        CONF=/config/qBittorrent/config/qBittorrent.conf
        if [ ! -f "$$CONF" ]; then
          echo "[felhom] first boot - seeding qBittorrent config and the generated WebUI password"
          mkdir -p /config/qBittorrent/config
          HASH=$$(QBT_PW="$$QBT_ADMIN_PASSWORD" python3 -c "import base64,hashlib,os,sys; s=os.urandom(16); k=hashlib.pbkdf2_hmac('sha512', os.environ['QBT_PW'].encode('utf-8'), s, 100000, 64); sys.stdout.write('@ByteArray(%s:%s)' % (base64.b64encode(s).decode(), base64.b64encode(k).decode()))")
          printf '%s\n' '[BitTorrent]' 'Session\DefaultSavePath=/downloads' 'Session\TempPath=/downloads/.temp' 'Session\TempPathEnabled=true' 'Session\Port=6881' '[Meta]' 'MigrationVersion=9999' '[Preferences]' 'WebUI\Username=admin' "WebUI\\Password_PBKDF2=\"$$HASH\"" 'WebUI\Port=8080' 'WebUI\Address=*' "WebUI\\ServerDomains=$$QBT_PUBLIC_HOST" 'WebUI\ReverseProxySupportEnabled=true' 'WebUI\TrustedReverseProxiesList=172.16.0.0/12' > "$$CONF"
        fi
        exec /sbin/tini -g -- /entrypoint.sh
    environment:
      PGID: "1000"
      PUID: "1000"
      QBT_ADMIN_PASSWORD: <redacted>
      QBT_LEGAL_NOTICE: confirm
      QBT_PUBLIC_HOST: torrent.bench.local
      QBT_TORRENTING_PORT: "6881"
      QBT_WEBUI_PORT: "8080"
      TZ: Europe/Budapest
      UMASK: "022"
    healthcheck:
      test:
        - CMD
        - curl
        - -fsS
        - -o
        - /dev/null
        - http://127.0.0.1:8080/
      timeout: 5s
      interval: 30s
      retries: 3
      start_period: 30s
    image: qbittorrentofficial/qbittorrent-nox:5.2.4-1
    labels:
      traefik.enable: "true"
      traefik.http.routers.qbittorrent.entrypoints: websecure
      traefik.http.routers.qbittorrent.rule: Host(`torrent.bench.local`)
      traefik.http.routers.qbittorrent.tls: "true"
      traefik.http.routers.qbittorrent.tls.certresolver: letsencrypt
      traefik.http.services.qbittorrent.loadbalancer.server.port: "8080"
    networks:
      traefik-public: null
    restart: unless-stopped
    volumes:
      - type: bind
        source: /opt/qbt-bench/userdata/media/torrents
        target: /downloads
        bind:
          create_host_path: true
      - type: volume
        source: qbittorrent_config
        target: /config
        volume: {}
networks:
  traefik-public:
    name: traefik-public
    external: true
volumes:
  qbittorrent_config:
    name: qbt-bench_qbittorrent_config
