# syntax=docker/dockerfile:1
# Builds the engine workspace with the pinned toolchain. Every base is
# digest-pinned. `docker/control.lock` records the same three references, and
# 08_image compares the image labels with it. Bundle images are built
# separately from each bundle's own Dockerfile; this control image contains no
# plugin binary and no engine library.
ARG RUST_IMAGE=rust:1.99.1-bookworm@sha256:e6f2cff35d0f309acf8433b4f05878ed77092602c5065cc81801177da0bb3a9c
ARG DEBIAN_IMAGE=debian:bookworm-slim@sha256:734a72b3d6142a71993b4bc580ca01d8ca687f77a2c422d96a961d3376b179bc
ARG UV_IMAGE=ghcr.io/astral-sh/uv:0.13.4@sha256:9f130421e930bfbdc563c00740466603f8723158e90297d07957c2294640262b
FROM ${RUST_IMAGE} AS build
WORKDIR /src
# The manifests and the lock file decide the dependency graph. The registry
# cache and the target cache are BuildKit mounts: a source change rebuilds the
# layer, but downloaded crates and unchanged compiled dependencies survive it.
COPY Cargo.toml Cargo.lock rust-toolchain.toml ./
# The root workspace names the engine crates. Each bundle is its own workspace
# and its own image; the control build neither compiles a plugin nor copies an
# engine library.
COPY crates ./crates
RUN --mount=type=cache,target=/usr/local/cargo/registry \
    --mount=type=cache,target=/cache/target \
    CARGO_TARGET_DIR=/cache/target cargo build --locked --release --workspace \
    && install -d /out/bin \
    && install -m 0755 /cache/target/release/inf3r-web /out/bin/inf3r-web \
    && install -m 0755 /cache/target/release/inf3r /out/bin/inf3r \
    && install -m 0755 /cache/target/release/inf3r-probe /out/bin/inf3r-probe

# The runtime image holds the control binaries and no plugin binary and no
# engine library. An engine is a sibling container from its own bundle image.
# `uv` runs the Python engine of the echo bundle, and the system Python keeps it
# from downloading an interpreter at request time.
FROM ${UV_IMAGE} AS uv

FROM ${DEBIAN_IMAGE} AS control
ARG RUST_IMAGE
ARG DEBIAN_IMAGE
ARG UV_IMAGE
LABEL org.inf3r.control.base.rust="${RUST_IMAGE}" \
      org.inf3r.control.base.debian="${DEBIAN_IMAGE}" \
      org.inf3r.control.base.uv="${UV_IMAGE}"
# python3 is the healthcheck and the echo fixture runtime; procps supplies the
# pgrep that the compose proof uses to show no plugin process is in the control.
# The state directory is the only path that the server writes.
RUN apt-get update \
    && apt-get install -y --no-install-recommends python3 procps \
    && rm -rf /var/lib/apt/lists/* \
    && install -d /var/lib/inf3r \
    && install -d /opt/inf3r/bin /opt/inf3r/plugins /opt/inf3r/plugins-extra /etc/inf3r \
    && touch /opt/inf3r/images.lock
# The operator may mount another deploy file over this one.
COPY --from=build /out/bin/ /usr/local/bin/
# The control reads bundle manifests; it never receives a plugin binary or an
# engine library. The compose service builds this target only.
COPY plugins/mock/x64-cpu/bundle.toml plugins/mock/x64-cpu/target.toml plugins/mock/x64-cpu/BUNDLE.md /opt/inf3r/plugins/mock/x64-cpu/
COPY plugins/mock/x64-cpu/models /opt/inf3r/plugins/mock/x64-cpu/models
COPY --from=uv /uv /usr/local/bin/uv
COPY docker/config/deploy.toml /etc/inf3r/deploy.toml
# The entry point writes the first bearer token into the state volume, so
# `docker compose up` serves without a secret baked into the image.
COPY docker/entrypoint.sh /usr/local/bin/inf3r-entrypoint
RUN chmod 0755 /usr/local/bin/inf3r-entrypoint
# The control runs as container root. Rootless Docker maps that root to the
# unprivileged host uid, so it is not host root; it matches the root-mapped
# engine containers that must connect to the control's private sockets (D28).
USER 0:0
EXPOSE 8080
ENTRYPOINT ["/usr/local/bin/inf3r-entrypoint"]
