0Pricing
Docker & DevOps Fundamentals · Lesson

Multi-Stage Builds for Efficiency

Utilize multi-stage builds to create smaller, more secure production images by separating build and runtime dependencies.

Multi-Stage Builds for Efficiency is a free Docker & DevOps Fundamentals lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Docker & DevOps Fundamentals learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Intro to Multi-Stage Builds

Welcome to advanced Dockerfile optimization! Today, we'll explore multi-stage builds, a powerful technique to create smaller, more efficient Docker images.

Smaller images lead to faster downloads, improved security, and better resource utilization.

Multi-Stage Builds for Efficiency — illustration 1

Why Smaller Images Matter

When you build a Docker image, it often includes tools and dependencies only needed during the build process, not for the final application runtime.

  • Faster pulls: Smaller images download quicker.
  • Less disk space: Saves storage on registries and hosts.
  • Reduced attack surface: Fewer components mean fewer potential vulnerabilities.
  • Improved security: Your production image only contains what's essential.

The Single-Stage Problem

Consider a compiled application, like one written in Go. A traditional, single-stage Dockerfile would include the entire Go compiler and development dependencies in the final image.

This makes the final image much larger than necessary, as the compiler isn't needed to run the compiled binary.

Single-Stage Example (Go)

Here's a simple Go application and a Dockerfile that builds it in a single stage. Notice how the build environment is included in the final image.

main.go:

package main

import "fmt"

func main() {
    fmt.Println("Hello from Single-Stage Docker!")
}

Single-Stage Dockerfile

This Dockerfile uses `golang:1.21-alpine` as its base. After building the app, this large base image, along with all its build tools, remains in the final image.

FROM golang:1.21-alpine
WORKDIR /app
COPY main.go .
RUN go build -o myapp main.go
EXPOSE 8080
CMD ["./myapp"]

Anatomy of Multi-Stage Builds

Multi-stage builds solve this by allowing you to define multiple `FROM` instructions in a single Dockerfile. Each `FROM` starts a new build stage.

  • Use the AS keyword to name a stage (e.g., FROM golang:1.21-alpine AS builder).
  • You can then copy artifacts from a previous stage using COPY --from=<stage_name>.

Only the final stage is saved as the image.

Multi-Stage Structure

A typical multi-stage Dockerfile has at least two stages:

  • Build Stage: Contains all tools and source code needed to compile your application.
  • Runtime Stage: A minimal base image that only includes the compiled application and its runtime dependencies.

The build stage's intermediate image is discarded, leaving a lean final image.

Multi-Stage Example (Go)

Let's rewrite our Go application's Dockerfile using a multi-stage approach. We'll build the app in the first stage and then copy only the compiled binary to a much smaller `alpine` base image.

# Stage 1: Build the application
FROM golang:1.21-alpine AS builder
WORKDIR /app
COPY main.go .
RUN go build -o myapp main.go

# Stage 2: Create the final lightweight image
FROM alpine:latest
WORKDIR /app
COPY --from=builder /app/myapp .
EXPOSE 8080
CMD ["./myapp"]

Running Multi-Stage Build

When you build this Dockerfile, Docker executes each stage. However, only the content of the last `FROM` instruction, combined with files copied from previous stages, forms the final image.

This results in a significantly smaller image compared to the single-stage build, as the Go compiler and build tools are left behind.

Benefits of Multi-Stage Builds

Multi-stage builds are incredibly useful for:

  • Compiled Languages: Go, Java, C++, Rust.
  • Frontend Frameworks: Building React/Angular apps and serving static assets with Nginx.
  • Node.js Applications: Installing `node_modules` in a build stage, then copying only `package.json`, `package-lock.json`, and the built app to a slim runtime image.

Check Your Knowledge

Which of the following are primary benefits of using multi-stage Docker builds?

Recap: Multi-Stage Efficiency

You've learned about multi-stage Docker builds!

  • They involve multiple FROM instructions in a single Dockerfile.
  • The AS keyword names stages, and COPY --from= transfers artifacts.
  • The key benefit is drastically smaller, more secure, and efficient final images.

This technique is crucial for optimizing your containerized applications. Next, we'll look at leveraging build caching!

Frequently asked questions

Is the “Multi-Stage Builds for Efficiency” lesson free?

Yes — the full text of “Multi-Stage Builds for Efficiency” is free to read here on the web, and the Docker & DevOps Fundamentals course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Docker & DevOps Fundamentals course, upgrade to CoddyKit PRO.

What will I learn in “Multi-Stage Builds for Efficiency”?

Utilize multi-stage builds to create smaller, more secure production images by separating build and runtime dependencies. You practise Docker & DevOps Fundamentals with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Docker & DevOps Fundamentals?

No prior experience is required. Docker & DevOps Fundamentals on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Multi-Stage Builds for Efficiency” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Docker & DevOps Fundamentals lesson?

Yes. Every Docker & DevOps Fundamentals lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Multi-Stage Builds for Efficiency
  2. Leveraging Build Caching
  3. Dockerfile Security Best Practices
  4. Minimizing Image Size with Slim and Distroless Bases
← Back to Docker & DevOps Fundamentals