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
























