# media-sidecar — FFmpeg-based metadata scrubber for DChain node.
#
# Build:   docker build -t dchain/media-sidecar -f docker/media-sidecar/Dockerfile .
# Run:     docker run -p 8090:8090 dchain/media-sidecar
# Compose: see docker-compose.yml; node points DCHAIN_MEDIA_SIDECAR_URL at it.
#
# Stage 1 — build a tiny static Go binary.
FROM golang:1.22-alpine AS build
WORKDIR /src
# Copy only what we need (the sidecar main is self-contained, no module
# deps on the rest of the repo, so this is a cheap, cache-friendly build).
COPY docker/media-sidecar/main.go ./main.go
RUN go mod init dchain-media-sidecar 2>/dev/null || true
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o /out/media-sidecar ./main.go

# Stage 2 — runtime with ffmpeg. Alpine has a lean ffmpeg build (~90 MB
# total image, most of it codecs we actually need).
FROM alpine:3.19
RUN apk add --no-cache ffmpeg ca-certificates \
 && addgroup -S dchain && adduser -S -G dchain dchain
COPY --from=build /out/media-sidecar /usr/local/bin/media-sidecar

USER dchain
EXPOSE 8090

# Pin sensible defaults; operator overrides via docker-compose env.
ENV LISTEN_ADDR=:8090 \
    FFMPEG_BIN=ffmpeg \
    MAX_INPUT_MB=32 \
    JOB_TIMEOUT_SECS=60

HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \
    CMD wget -qO- http://127.0.0.1:8090/healthz || exit 1

ENTRYPOINT ["/usr/local/bin/media-sidecar"]
