Skip to content

Commit

Permalink
Use cargo chef to cache dependencies between builds
Browse files Browse the repository at this point in the history
  • Loading branch information
The-Fireplace committed Apr 7, 2024
1 parent 0043a5a commit 60a3f6c
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# 1. Build rust
FROM rust:1.76-bookworm as build
FROM lukemathwalker/cargo-chef:0.1.66-rust-1-slim-bookworm AS chef

RUN update-ca-certificates

Expand All @@ -16,36 +16,31 @@ RUN adduser \
--uid "${UID}" \
"${USER}"

# Create a new empty shell project
RUN cargo new --bin httpserve
WORKDIR /httpserve

# Copy manifests
COPY Cargo.lock ./Cargo.lock
COPY Cargo.toml ./Cargo.toml
FROM chef AS planner
COPY . .
RUN cargo chef prepare --recipe-path recipe.json

# Build dependencies to cache them
RUN cargo build --release
RUN rm src/*.rs

# Update build directory's source code
COPY src ./src

# Build for release
RUN rm -f ./target/release/deps/httpserve*
RUN cargo install --path .
FROM chef AS builder
COPY --from=planner /httpserve/recipe.json recipe.json
# Build dependencies - this is the caching Docker layer!
RUN cargo chef cook --release --recipe-path recipe.json
# Build application
COPY . .
RUN cargo build --release --bin httpserve

# 2. Package in a small production image
FROM gcr.io/distroless/cc-debian12

# Import user from builder.
COPY --from=build /etc/passwd /etc/passwd
COPY --from=build /etc/group /etc/group
COPY --from=chef /etc/passwd /etc/passwd
COPY --from=chef /etc/group /etc/group

WORKDIR /web

# copy the build artifact from the build stage
COPY --from=build /httpserve/target/release/httpserve ./httpserve
COPY --from=builder /httpserve/target/release/httpserve ./httpserve

EXPOSE 80

Expand Down

0 comments on commit 60a3f6c

Please sign in to comment.