What if you could build your app and deliver a tiny, clean image without messy manual cleanup?
Why Multi-stage builds concept in Docker? - Purpose & Use Cases
Imagine you want to create a small, fast Docker image for your app, but your app needs many tools to build. You first install all tools, build the app, then try to remove the tools manually before sharing the image.
This manual way is slow and messy. You might forget to remove some tools, making the image big and slow to download. Also, cleaning up by hand is error-prone and wastes time.
Multi-stage builds let you use one Dockerfile to build your app in one stage with all tools, then copy only the final app to a clean, small image in the next stage. This keeps images tiny and build steps clear.
FROM ubuntu RUN apt-get update && apt-get install -y build-essential RUN build-app RUN apt-get remove -y build-essential CMD ./app
FROM ubuntu AS build RUN apt-get update && apt-get install -y build-essential RUN build-app FROM scratch COPY --from=build /app /app CMD ["/app"]
It enables creating small, efficient Docker images by separating build and runtime environments cleanly.
Developers building a Go app use multi-stage builds to compile the app with all tools, then produce a tiny final image with just the compiled binary, perfect for fast deployment.
Manual cleanup of build tools is slow and error-prone.
Multi-stage builds separate build and runtime steps in one Dockerfile.
Final images become smaller, faster, and easier to manage.
Practice
Solution
Step 1: Understand multi-stage build concept
Multi-stage builds allow splitting the Dockerfile into multiple stages, separating build tools from the final runtime environment.Step 2: Identify the benefit
This separation helps reduce image size by excluding unnecessary build dependencies from the final image.Final Answer:
To create smaller and cleaner Docker images by separating build and runtime stages -> Option CQuick Check:
Multi-stage builds = smaller images [OK]
- Confusing multi-stage builds with running multiple containers
- Thinking it speeds up Docker daemon startup
- Believing it auto-updates images
Solution
Step 1: Recall Dockerfile syntax for naming stages
The correct syntax to name a build stage is usingFROM <image> AS <stage-name>.Step 2: Match options with syntax
FROM node AS builder matches this syntax exactly, while others use invalid keywords.Final Answer:
FROM node AS builder -> Option BQuick Check:
Stage naming uses FROM ... AS ... [OK]
- Using BUILD or STAGE keywords instead of FROM
- Trying to name stages with COPY command
- Missing AS keyword
FROM golang:1.20 AS builder WORKDIR /app COPY . . RUN go build -o myapp FROM alpine:latest COPY --from=builder /app/myapp /usr/local/bin/myapp CMD ["myapp"]
Solution
Step 1: Analyze the multi-stage build steps
The first stage builds the Go binary using the full Go environment. The second stage uses a minimal Alpine image and copies only the binary from the builder stage.Step 2: Understand image size impact
Because only the binary is copied, the final image excludes the Go build tools and source code, making it small.Final Answer:
The final image will be small because only the built binary is copied from the builder stage -> Option AQuick Check:
Copying only binary = smaller final image [OK]
- Assuming the entire build environment is included in final image
- Thinking no files are copied
- Believing source code is copied without binary
FROM node:18 AS build WORKDIR /app COPY package.json . RUN npm install COPY . . RUN npm run build FROM nginx:alpine COPY --from=build /app/dist /usr/share/nginx/html RUN npm install CMD ["nginx", "-g", "daemon off;"]
Solution
Step 1: Review commands in each stage
The first stage builds the app correctly. The second stage uses nginx image, which does not have npm installed.Step 2: Identify invalid command in second stage
Running 'npm install' in the nginx stage will cause an error because npm is not present.Final Answer:
The second stage incorrectly runs 'npm install' which is not available in nginx image -> Option DQuick Check:
npm commands require node environment, not nginx [OK]
- Assuming npm works in nginx image
- Missing AS keyword (actually present here)
- Misreading COPY syntax
- Confusing CMD syntax
Solution
Step 1: Understand multi-stage build goals
The first stage installs dependencies and runs tests. The second stage copies the app and dependencies for runtime only.Step 2: Analyze each option
FROM python:3.12 AS builder WORKDIR /app COPY requirements.txt . RUN python -m venv venv && venv/bin/pip install -r requirements.txt COPY . . RUN venv/bin/pytest FROM python:3.12-slim WORKDIR /app COPY --from=builder /app /app CMD ["venv/bin/python", "app.py"]
installs dependencies in a venv and tests in builder, then copies all of /app (including venv) to slim image without reinstalling.FROM python:3.12 WORKDIR /app COPY requirements.txt . RUN pip install -r requirements.txt COPY . . RUN pytest CMD ["python", "app.py"]
has no multi-stage build.FROM python:3.12 AS builder WORKDIR /app COPY . . RUN pytest FROM python:3.12-slim WORKDIR /app COPY --from=builder /app /app RUN pip install -r requirements.txt CMD ["python", "app.py"]
installs dependencies after copying, which is inefficient.FROM python:3.12 AS builder WORKDIR /app COPY requirements.txt . RUN pip install -r requirements.txt COPY . . RUN pytest FROM python:3.12-slim WORKDIR /app COPY --from=builder /app /app RUN pip install -r requirements.txt CMD ["python", "app.py"]
reinstalls dependencies in second stage, increasing image size.Final Answer:
Uses virtualenv in builder stage to install dependencies and run tests, copies entire /app including venv to slim runtime -> Option AQuick Check:
Install and test in builder, copy to slim runtime [OK]
- Not using multi-stage builds
- Reinstalling dependencies in final stage
- Running tests in final image
- Copying unnecessary files
Ready to prove it?
Pass the test and this pattern moves unknown
