Skip to content

Instantly share code, notes, and snippets.

@jbg
Last active May 1, 2021 05:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbg/dc7d271f7da261e424393c73b73024a1 to your computer and use it in GitHub Desktop.
Save jbg/dc7d271f7da261e424393c73b73024a1 to your computer and use it in GitHub Desktop.
multi-stage Dockerfile for rust builds that allows building stages after `cargo fetch` without network access
# Example usage:
# docker build --target=fetcher .
# docker build --network=none -t "$TAG" .
FROM rust-builder:0.2.1 AS fetcher
COPY Cargo.toml Cargo.lock ./
RUN mkdir src \
&& touch src/main.rs \
&& cargo fetch --locked
# The following stages can be built without network access
FROM rust-builder:0.2.1 AS builder
COPY . .
COPY --from=fetcher /usr/local/cargo/ /usr/local/cargo/
RUN cargo lichking check \
&& cargo +nightly fmt -- --check --files-with-diff \
&& cargo clippy -- -D warnings \
&& cargo build --release --frozen
FROM alpine-runtime:0.3.0
COPY --from=builder /target/release/program-name /usr/local/bin/
USER app
ENTRYPOINT ["/usr/local/bin/program-name"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment