liveBook · Manning

Container image

As explained in Rust Servers, Services, and Apps

A Docker image is an immutable package containing a filesystem, metadata, and application dependencies from which containers can be created.

A Docker image is a reusable template for creating containers. A Dockerfile defines how to build it; the resulting image can be handed from development to operations and instantiated on a Docker host. section 12.1.2

Start with a Rust web service

Create a Rust binary project with Actix Web routes, then verify it locally with `cargo run`. In the project root, `Dockerfile-basic` starts from `rust`, copies the project into `/app`, runs `cargo build --release`, and starts `./target/release/docker-rust`. Build it with `docker build -f Dockerfile-basic . -t docker-rust-basic`; run it with `docker run -p 8080:8080 -t docker-rust-basic`. Check both routes at `localhost:8080` and `localhost:8080/hello`. section 12.2.2

The basic image is 1.32 GB because it retains the Rust compiler and intermediate build artifacts alongside the application. Getting a working image first makes that cost visible; a multistage build addresses it next. section 12.2.2 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"]

Compare the two `FROM` stages: the Rust stage builds the release binary, while the distroless runtime stage copies only that binary and starts it. section 12.2.3

Build the smaller runtime image

Run `docker build -f Dockerfile-lite . -t docker-rust-lite`, then check `docker images`. The example falls from 1.32 GB to 31.8 MB. Run it with `docker run -p 8080:8080 -t docker-rust-lite` and test the same routes; the smaller image still has to serve requests. section 12.2.2 section 12.2.3

An image does not by itself specify an entire deployment. Docker Compose can build a web-service image, start its container alongside a container from `postgres:latest`, and supply environment variables. A volume persists Postgres data, while a shared Docker network lets the running services connect. section 12.3.1 section 12.5

How Rust Servers, Services, and Apps covers it

How deep it goes
Works through a basic Dockerfile, build and run commands, then a two-stage build measured by image size.
Examples
Uses an Actix Web server, a 1.32 GB basic image, a 31.8 MB runtime image, and a Postgres container.
What it assumes
Assumes a Rust web-service project but starts Docker work by checking the installation and running a sample image.
What you can do after
Build it from scratch with a Dockerfile, run and test its container, then reduce its size with a multistage build.
How it connects
Connects Dockerfiles and containers to multistage builds, Docker Compose, Postgres, volumes, and container networks.

Also explained in

Compare how 25 books explain container image →

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.