-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
executable file
·70 lines (52 loc) · 2.03 KB
/
Dockerfile
File metadata and controls
executable file
·70 lines (52 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Dockerfile for Rust Boilerplate
#
# Inspired by:
# - https://depot.dev/blog/rust-dockerfile-best-practices
# - https://depot.dev/docs/languages/rust-dockerfile
# - https://www.lpalmieri.com/posts/fast-rust-docker-builds/
# - https://github.com/LukeMathWalker/cargo-chef
ARG RUST_VERSION=${RUST_VERSION:-"1.91"}
ARG RUST_TARGET=${RUST_TARGET:-"x86_64-unknown-linux-musl"}
FROM rust:${RUST_VERSION}-slim AS build-base
WORKDIR /app
ARG RUST_TARGET
# Install only minimal dependencies
RUN apt-get update -y \
&& apt-get install -y --no-install-recommends \
ca-certificates \
musl-tools \
curl \
binutils \
&& rm -rf /var/lib/apt/lists/*
# Install cargo-binstall (much faster and smaller than cargo install)
RUN curl -fsSL https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh \
| bash
# Add the target required for building artifacts
RUN rustup target add "${RUST_TARGET}" \
&& cargo binstall --no-confirm --force \
cargo-chef \
sccache \
&& cargo cache -a || true \
&& rm -rf ~/.cargo/registry ~/.cargo/git
ENV RUSTC_WRAPPER=sccache SCCACHE_DIR=/sccache
FROM build-base AS planner
COPY . .
RUN cargo chef prepare --recipe-path recipe.json
FROM build-base AS builder
ARG RUST_TARGET
COPY --from=planner /app/recipe.json .
RUN --mount=type=cache,target=/usr/local/cargo/registry \
--mount=type=cache,target=/usr/local/cargo/git \
--mount=type=cache,target=$SCCACHE_DIR,sharing=locked \
cargo chef cook --release --target $RUST_TARGET --recipe-path recipe.json
COPY . .
RUN --mount=type=cache,target=/usr/local/cargo/registry \
--mount=type=cache,target=/usr/local/cargo/git \
--mount=type=cache,target=$SCCACHE_DIR,sharing=locked \
cargo build --release --target $RUST_TARGET
RUN strip /app/target/$RUST_TARGET/release/rust-base
FROM gcr.io/distroless/static-debian12:nonroot AS runtime
WORKDIR /app
ARG RUST_TARGET
COPY --from=builder /app/target/$RUST_TARGET/release/rust-base /usr/local/bin/app
CMD ["/usr/local/bin/app"]