liveBook · Manning

Multistage build

As explained in Rust Servers, Services, and Apps

A multistage Docker build uses multiple Dockerfile stages so that only selected artifacts from earlier stages are included in the final image.

A multistage Docker build uses multiple FROM stages to separate compilation from runtime packaging. Named stages let the final image copy only selected artifacts from an earlier stage, excluding compilers and intermediate files. section 12.2.3 section 12.6

Rust release image

Build the release binary with the Rust image, then use a Google Distroless runtime image and copy /app/target/release/docker-rust into it. Build with Dockerfile-lite, run docker-rust-lite on port 8080, and verify the greeting endpoints. The resulting image is reported as 31.8 MB. section 12.2.3

# Use the main rust Docker image
FROM rust as build
 
# copy app into Docker image
COPY . /app
 
# Set the workdirectory
WORKDIR /app
 
# build the app
RUN cargo build --release
 
# use google distroless as runtime image
FROM gcr.io/distroless/cc-debian11
 
# copy app from builder
COPY --from=build /app/target/release/docker-rust /app/docker-rust
WORKDIR /app
 
# start the application
CMD ["./docker-rust"]

Shows the complete two-stage Dockerfile, including the builder stage, release compilation, runtime image, artifact copy, and startup command. section 12.2.3

How Rust Servers, Services, and Apps covers it

How deep it goes
Explains multiple FROM stages, named stages, artifact copying, image-size verification, runtime testing, and security and caching benefits.
Examples
Uses a Rust builder, Google Distroless runtime, Dockerfile-lite, a 31.8 MB image, and localhost greeting endpoints.
What it assumes
Assumes Docker installation and a Rust project, while demonstrating Dockerfile creation, image building, and container execution.
What you can do after
Build and run a smaller Rust Docker image using named stages and a runtime-only artifact.
How it connects
Connects Dockerfiles, Docker images, Rust release binaries, static binaries, and runtime images.

Also explained in

Compare how 2 books explain multistage build →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust Servers, Services, and Apps. The section links point to the parts of the book it draws on.