# Dockerfile.plain
ARG BASE_IMAGE=rust:1.52.1-slim-buster
FROM $BASE_IMAGE
WORKDIR app
COPY . .
RUN cargo build --release
CMD ["./target/release/hello"]
time docker build -f Dockerfile.plain -t hello:0.1.0 .
# build
real 12m13.412s
user 0m0.135s
sys 0m0.095s
REPOSITORY TAG IMAGE ID CREATED SIZE
hello 0.1.0 ac4e1a72ba05 2 minutes ago 1.38GB
rust 1.52.1-slim-buster 61cb3c65a6ba 3 weeks ago 621MB
# Dockerfile.chef
ARG BASE_IMAGE=rust:1.52.1-slim-buster
FROM $BASE_IMAGE as planner
WORKDIR app
RUN cargo install cargo-chef --version 0.1.20
COPY . .
RUN cargo chef prepare --recipe-path recipe.json
FROM $BASE_IMAGE as cacher
WORKDIR app
RUN cargo install cargo-chef --version 0.1.20
COPY --from=planner /app/recipe.json recipe.json
RUN cargo chef cook --release --recipe-path recipe.json
FROM $BASE_IMAGE as builder
WORKDIR app
COPY . .
# Copy over the cached dependencies
COPY --from=cacher /app/target target
COPY --from=cacher $CARGO_HOME $CARGO_HOME
RUN cargo build --release
FROM $BASE_IMAGE
COPY --from=builder /app/target/release/hello /
CMD ["./hello"]
# first test omitted
# second test
real 19m32.836s
user 0m0.188s
sys 0m0.088s
# third test
real 1m52.982s
user 0m0.060s
sys 0m0.088s
REPOSITORY TAG IMAGE ID CREATED SIZE
hello 0.1.0 ca1a9e1e5948 About a minute ago 628MB
# Dockerfile.musl
ARG BASE_IMAGE=rust:1.52.1-slim-buster
FROM $BASE_IMAGE as planner
WORKDIR app
RUN cargo install cargo-chef --version 0.1.20
COPY . .
RUN cargo chef prepare --recipe-path recipe.json
FROM $BASE_IMAGE as cacher
WORKDIR app
RUN cargo install cargo-chef --version 0.1.20
COPY --from=planner /app/recipe.json recipe.json
RUN cargo chef cook --release --recipe-path recipe.json
FROM $BASE_IMAGE as builder
WORKDIR app
COPY . .
# Copy over the cached dependencies
COPY --from=cacher /app/target target
COPY --from=cacher $CARGO_HOME $CARGO_HOME
# We need static linking for musl
RUN rustup target add x86_64-unknown-linux-musl
# `cargo build` doesn't work in static linking, need `cargo install`
RUN cargo install --target x86_64-unknown-linux-musl --path .
FROM scratch
COPY --from=builder /usr/local/cargo/bin/hello .
CMD ["./hello"]
# first test ommitted
# second test
real 9m22.049s
user 0m0.113s
sys 0m0.103s
# third test
real 9m46.035s
user 0m0.120s
sys 0m0.085s
REPOSITORY TAG IMAGE ID CREATED SIZE
hello 0.1.0 332ce3b4f717 30 seconds ago 8.38MB
# Dockerfile.distroless
ARG BASE_IMAGE=rust:1.52.1-slim-buster
FROM $BASE_IMAGE as planner
WORKDIR app
RUN cargo install cargo-chef --version 0.1.20
COPY . .
RUN cargo chef prepare --recipe-path recipe.json
FROM $BASE_IMAGE as cacher
WORKDIR app
RUN cargo install cargo-chef --version 0.1.20
COPY --from=planner /app/recipe.json recipe.json
RUN cargo chef cook --release --recipe-path recipe.json
FROM $BASE_IMAGE as builder
WORKDIR app
COPY . .
# Copy over the cached dependencies
COPY --from=cacher /app/target target
COPY --from=cacher $CARGO_HOME $CARGO_HOME
RUN cargo build --release
FROM gcr.io/distroless/cc-debian10
COPY --from=builder /app/target/release/hello /
CMD ["./hello"]
# first test ommitted
# second test
real 1m57.033s
user 0m0.045s
sys 0m0.038s
# third test
real 2m12.585s
user 0m0.093s
sys 0m0.096s
REPOSITORY TAG IMAGE ID CREATED SIZE
hello 0.1.0 118da37bdfe7 25 seconds ago 29MB
From 日报小组 侯盛鑫,坏姐姐
原文作者:Rust语言中文社区;如有侵权,请联系管理员删除。