Files
Media-download-telegram-bot/deploy/Dockerfile
T
MichilisandClaude Fable 5.2 7d4893bc3f Phase 4: scale out
Remote workers, Postgres and S3, all chosen by configuration.

- Pull-based worker protocol over HTTP: claim (long poll), heartbeat,
  complete, fail, upload, source, probe. Workers need no inbound ports
- cmd/worker: standalone stateless worker with health endpoint and
  scheduled yt-dlp self-update
- Per-worker bearer tokens, stored hashed, revocable; identity always
  taken from the token; actions only on jobs the worker holds a lease for
- Tags: site_tags routes a site's jobs and lookups to workers that carry
  the required tags
- Drain, /workers, /ytdlp update, and "core worker add|revoke|list"
- Link lookups can run on remote workers (PROBE_MODE), so the core needs
  no yt-dlp and no residential address
- Postgres backend with pgx, same schema and queries; per-user advisory
  lock and job row lock where SQLite's serial writes were relied on
- The whole test suite runs on SQLite and on Postgres
- S3 storage backend with presigned worker uploads, sharing a conformance
  suite with the local backend
- Dockerfile with core, worker and faketelegram targets, docker-compose
  with optional Postgres and local Bot API server, systemd units
- deploy/acceptance/kill-worker.sh: kills a worker mid-job and checks
  that the other one delivers the file
- Fixes found on the way: /cancel could let the next playlist item start;
  a refund note could be missing when the sweep refunded first

Docker was not available on the build machine: the compose files were
validated with "docker compose config" only. The acceptance scenario was
run with real processes.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-18 09:27:11 +00:00

86 lines
3.2 KiB
Docker

# syntax=docker/dockerfile:1
#
# One Dockerfile, three targets:
#
# docker build -f deploy/Dockerfile --target core -t dlbot-core .
# docker build -f deploy/Dockerfile --target worker -t dlbot-worker .
# docker build -f deploy/Dockerfile --target faketelegram -t dlbot-faketelegram .
#
# docker-compose.yml builds core and worker for you.
ARG GO_VERSION=1.26
ARG DEBIAN_VERSION=bookworm
# --- build -------------------------------------------------------------------
FROM golang:${GO_VERSION}-${DEBIAN_VERSION} AS build
WORKDIR /src
# go.mod asks for a specific Go patch release. Let the toolchain fetch it
# when the image is a little older.
ENV GOTOOLCHAIN=auto CGO_ENABLED=0
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN go build -trimpath -ldflags "-s -w" -o /out/core ./cmd/core \
&& go build -trimpath -ldflags "-s -w" -o /out/worker ./cmd/worker \
&& go build -trimpath -ldflags "-s -w" -o /out/faketelegram ./cmd/faketelegram
# --- runtime base: ffmpeg and yt-dlp -----------------------------------------
FROM debian:${DEBIAN_VERSION}-slim AS runtime
ARG TARGETARCH
# "latest", or a release tag such as 2026.10.26 to pin the version.
ARG YTDLP_VERSION=latest
RUN apt-get update \
&& apt-get install -y --no-install-recommends ca-certificates curl ffmpeg tini \
&& rm -rf /var/lib/apt/lists/* \
&& useradd --system --uid 10001 --home-dir /data --shell /usr/sbin/nologin dlbot \
&& mkdir -p /data /opt/dlbot/bin \
&& case "${TARGETARCH:-amd64}" in \
arm64) asset=yt-dlp_linux_aarch64 ;; \
*) asset=yt-dlp_linux ;; \
esac \
&& if [ "$YTDLP_VERSION" = "latest" ]; then \
url="https://github.com/yt-dlp/yt-dlp/releases/latest/download/$asset"; \
else \
url="https://github.com/yt-dlp/yt-dlp/releases/download/$YTDLP_VERSION/$asset"; \
fi \
&& curl -fsSL -o /opt/dlbot/bin/yt-dlp "$url" \
&& chmod 0755 /opt/dlbot/bin/yt-dlp \
# yt-dlp updates itself in place, so its directory belongs to the service user.
&& chown -R dlbot:dlbot /data /opt/dlbot
ENV PATH="/opt/dlbot/bin:${PATH}" \
YTDLP_PATH=/opt/dlbot/bin/yt-dlp \
FFMPEG_PATH=ffmpeg
USER dlbot
WORKDIR /data
ENTRYPOINT ["/usr/bin/tini", "--"]
# --- core --------------------------------------------------------------------
FROM runtime AS core
COPY --from=build /out/core /usr/local/bin/core
ENV HTTP_LISTEN=:8080 \
DB_DSN=/data/dlbot.db \
STORAGE_LOCAL_DIR=/data/files \
WORKER_TEMP_DIR=/data/tmp
VOLUME ["/data"]
EXPOSE 8080
HEALTHCHECK --interval=15s --timeout=5s --start-period=20s --retries=3 \
CMD curl -fsS http://127.0.0.1:8080/healthz || exit 1
CMD ["core"]
# --- worker ------------------------------------------------------------------
FROM runtime AS worker
COPY --from=build /out/worker /usr/local/bin/worker
ENV WORKER_TEMP_DIR=/data/tmp \
WORKER_HEALTH_LISTEN=:8081
VOLUME ["/data"]
HEALTHCHECK --interval=15s --timeout=5s --start-period=20s --retries=3 \
CMD curl -fsS http://127.0.0.1:8081/healthz || exit 1
CMD ["worker"]
# --- fake Telegram, for trying the bot without a token -----------------------
FROM debian:${DEBIAN_VERSION}-slim AS faketelegram
COPY --from=build /out/faketelegram /usr/local/bin/faketelegram
USER 10001
EXPOSE 8090
CMD ["faketelegram", "-listen", ":8090"]