Skip to content
Lab 2 of 3

Lean Images with Multi-Stage Builds

A bloated single-stage image is leaking build secrets and shipping its toolchain to production. Prove the leak, rewrite the Dockerfile as a multi-stage build, and ship a smaller, clean image.
  • Intermediate
  • 45 min
  • Docker
  • Security
  • Dockerfile
  • CI/CD

Overview

A previous team shipped a Docker image that builds and serves a static site — and quietly bakes the database password, the full source tree, and the entire build toolchain into the production image. It 'works', which is exactly why nobody noticed.

You'll first prove the leak by reading the secret straight out of the running image, then fix it properly with a multi-stage build: one stage compiles, and a clean final stage copies only the finished artifact. Same output, a smaller image, and zero secrets — the single most important Dockerfile pattern in production.

What you will learn

  • Build an image from an existing Dockerfile and inspect its layers with docker history
  • Extract a secret from inside an image to show that deleted files persist in layers
  • Author a multi-stage Dockerfile that separates the build stage from the runtime stage
  • Verify the final image is smaller and contains no source or secrets
  • Run and smoke-test the hardened image

Before you start

  • Run & Configure the Docker Daemon first, or comfort running Docker
  • You can read a basic Dockerfile
  • Linux shell + sudo

Tasks

  1. 01Build the legacy image as-is
  2. 02Prove the secret leak
  3. 03Rewrite it as a multi-stage build
  4. 04Ship the lean image

Next in this course