FROM rust:1-bookworm AS builder
WORKDIR /app
COPY Cargo.toml Cargo.lock ./
RUN mkdir -p src && echo "fn main() {}" > src/main.rs
RUN cargo build --locked --release
COPY src ./src
RUN touch src/main.rs && cargo build --locked --release

FROM debian:bookworm-slim
RUN apt-get update \
    && apt-get install -y --no-install-recommends ca-certificates \
    && rm -rf /var/lib/apt/lists/* \
    && mkdir -p /cache /certs \
    && chown -R 10001:10001 /cache /certs
COPY --from=builder /app/target/release/drayage /usr/local/bin/drayage
USER 10001:10001
LABEL org.opencontainers.image.source=https://github.com/rvben/drayage
LABEL org.opencontainers.image.description="OCI registry pull-through cache"
LABEL org.opencontainers.image.licenses=MIT
VOLUME ["/cache"]
EXPOSE 5001
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
  CMD ["/usr/local/bin/drayage", "healthcheck", "--url", "http://127.0.0.1:5001/readyz"]
ENTRYPOINT ["/usr/local/bin/drayage"]
CMD ["serve", "--config", "/config.toml"]
