• Discover
  • Collections
  • Board
  • Create
  • Profile
  • Settings
Paths

Lesson 3: Multi-stage Builds and Image Optimization

Lesson 3: Multi-stage Builds and Image Optimization

3 min read

Introduction

Building Docker images can sometimes lead to large image sizes, especially for compiled languages or applications with many build dependencies. Large images consume more disk space, take longer to pull, and can increase security risks. This lesson introduces multi-stage builds, a powerful technique to create smaller, more efficient Docker images by separating build-time dependencies from runtime dependencies. We will also explore other image optimization strategies.

Key Concepts

The Problem with Single-Stage Builds

In a typical single-stage Dockerfile, all build tools, source code, and intermediate artifacts (like compilers or test frameworks) remain in the final image, even if they are not needed at runtime. This leads to unnecessarily bloated images.

Multi-stage Builds

Multi-stage builds allow you to use multiple FROM statements in your Dockerfile. Each FROM instruction starts a new build stage. You can selectively copy artifacts from one stage to another, leaving behind everything you don't need in the final image.

  • Mechanism: Define a build stage (e.g., FROM node:18-alpine AS builder) and then a final runtime stage (FROM node:18-alpine). Use COPY --from=builder to transfer only the necessary compiled artifacts or application code from the builder stage to the runtime stage.

Benefits of Multi-stage Builds

  • Smaller Image Sizes: Significantly reduces the final image size by discarding build-time tools and temporary files.

  • Improved Security: Less surface area for attacks due to fewer installed packages.

  • Faster Deployment: Smaller images are quicker to pull and deploy.

  • Clearer Dockerfiles: Separates build logic from runtime configuration, making Dockerfiles easier to read and maintain.

Other Image Optimization Techniques

  • Choose Smaller Base Images: Prefer alpine variants of images (e.g., node:18-alpine instead of node:18) as they are much smaller.

  • Combine RUN Commands: Chain multiple RUN commands using && and remove unnecessary files (e.g., apt-get clean) in the same RUN instruction. This reduces the number of layers and improves caching.

  • Use .dockerignore: Exclude files and directories not needed in the build context.

  • Minimize Layers: Group related operations into a single RUN command where possible.

Example/Code

Here's an example of a multi-stage Dockerfile for a Go application:

dockerfile
# Stage 1: Build the application FROM golang:1.20-alpine AS builder WORKDIR /app COPY go.mod go.sum ./ RUN go mod download COPY . . RUN CGO_ENABLED=0 GOOS=linux go build -o myapp . # Stage 2: Create the final lean image FROM alpine:latest WORKDIR /app COPY --from=builder /app/myapp . CMD ["./myapp"]

In this example, the builder stage compiles the Go application.

The final `alpine:

latest` image then only copies the compiled binary, resulting in a much smaller runtime image that doesn't include the Go compiler or development tools.

Summary/Key Takeaways

  • Multi-stage builds reduce image size by separating build-time dependencies from runtime requirements.

  • Use multiple FROM statements and COPY --from to transfer artifacts between stages.

  • Other optimization techniques include using smaller base images, combining RUN commands, and utilizing .dockerignore.

End of lesson
👏Well done!
Previous Lesson
Lesson 2: Building Custom Images
Next Lesson
Lesson 4: Advanced Image Management

Course Content

0% Complete0/20 Lessons

Lesson 1: Introduction to Containers and Docker

Lesson 2: Installing Docker and Basic Commands

Lesson 3: Docker Images and Registries

Lesson 4: Running Containers and Port Mapping

Quiz

Lesson 1: Understanding Dockerfiles

Lesson 2: Building Custom Images

Lesson 3: Multi-stage Builds and Image Optimization

Lesson 4: Advanced Image Management

Quiz

Course Content

0% Complete0/20 Lessons

Lesson 1: Introduction to Containers and Docker

Lesson 2: Installing Docker and Basic Commands

Lesson 3: Docker Images and Registries

Lesson 4: Running Containers and Port Mapping

Quiz

Lesson 1: Understanding Dockerfiles

Lesson 2: Building Custom Images

Lesson 3: Multi-stage Builds and Image Optimization

Lesson 4: Advanced Image Management

Quiz