# syntax=docker/dockerfile:1.7
# Faunosteo worker image (review Part IV, VIII.1). Build context = repository root:
#   docker build -f crates/worker/Dockerfile --target test .          # sandbox tests + seccomp derivation
#   docker build -f crates/worker/Dockerfile -t faunosteo_worker:dev .  # runtime image
# The binary is a static musl executable; the runtime image carries nothing else.

FROM rust:1.99.0-alpine3.24 AS base
# build-base: the C compiler for libwebp (libwebp-sys, D-R4), nothing else is compiled from C.
# rustfmt/clippy: listed in rust-toolchain.toml, absent from the image's minimal profile.
RUN apk add --no-cache build-base && rustup component add rustfmt clippy
WORKDIR /w
COPY Cargo.toml Cargo.lock rust-toolchain.toml rustfmt.toml clippy.toml ./
COPY .cargo .cargo
COPY crates crates

FROM base AS build
RUN --mount=type=cache,id=faunosteo_cargo_registry_musl,target=/usr/local/cargo/registry \
    --mount=type=cache,id=faunosteo_target_musl,target=/w/target \
    cargo build -p faunosteo-worker --release --locked \
    && cp target/release/faunosteo-worker /faunosteo-worker

# The tests and the derivation run on the release profile (LTO, panic = abort) plus the
# test-hooks feature, inside the default Docker seccomp profile (the inner filter is allowed
# by it): the binary under test differs from the shipped one by the feature-gated hooks only.
FROM base AS test
RUN apk add --no-cache strace
# Own target cache: the hooks build and the shipped build must never share target/release.
RUN --mount=type=cache,id=faunosteo_cargo_registry_musl,target=/usr/local/cargo/registry \
    --mount=type=cache,id=faunosteo_target_musl_test,target=/w/target \
    cargo test -p faunosteo-worker --release --features test-hooks --locked -- --test-threads=4
RUN --mount=type=cache,id=faunosteo_cargo_registry_musl,target=/usr/local/cargo/registry \
    --mount=type=cache,id=faunosteo_target_musl_test,target=/w/target \
    sh crates/worker/scripts/derive-seccomp.sh

# Debian 13: the distroless tags still published and updated (README); same nonroot
# user 65532, no shell, CA bundle at /etc/ssl/certs (R4.12, owner D1).
FROM gcr.io/distroless/static-debian13:nonroot AS runtime
COPY --from=build /faunosteo-worker /faunosteo-worker
USER nonroot
ENTRYPOINT ["/faunosteo-worker"]
CMD ["run", "--check"]
