Bird
Raised Fist0
Dockerdevops~20 mins

Multi-stage for different environments in Docker - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Multi-stage Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of multi-stage Docker build with environment-specific stages
Given this Dockerfile with two stages named dev and prod, what will be the output of docker build --target prod . when running the container?
Docker
FROM alpine AS dev
RUN echo "Development build" > /build.txt

FROM alpine AS prod
RUN echo "Production build" > /build.txt
CMD cat /build.txt

FROM prod
AProduction build
BError: No such file /build.txt
CDevelopment build
DEmpty output
Attempts:
2 left
💡 Hint
The final stage is prod, so the file content comes from that stage.
❓ Configuration
intermediate
2:00remaining
Correct multi-stage Dockerfile for dev and prod with shared base
Which Dockerfile correctly uses a shared base stage and separate dev and prod stages that add environment-specific files?
A
FROM node:18 AS base
WORKDIR /app
COPY package.json .
RUN npm install

FROM base
COPY dev.config.js prod.config.js .
CMD ["node", "app.js"]
B
FROM node:18
WORKDIR /app
COPY package.json .
RUN npm install
COPY dev.config.js .
CMD ["node", "app.js"]

FROM node:18
COPY prod.config.js .
CMD ["node", "app.js"]
C
FROM node:18 AS base
WORKDIR /app
COPY package.json .
RUN npm install

FROM base AS dev
COPY dev.config.js .
CMD ["node", "app.js"]

FROM base AS prod
COPY prod.config.js .
CMD ["node", "app.js"]
D
FROM node:18 AS base
WORKDIR /app
COPY package.json .
RUN npm install

FROM dev
COPY dev.config.js .
CMD ["node", "app.js"]

FROM prod
COPY prod.config.js .
CMD ["node", "app.js"]
Attempts:
2 left
💡 Hint
Look for a shared base stage and separate dev and prod stages that copy only their config files.
❓ Troubleshoot
advanced
2:00remaining
Why does the prod stage fail to find a file from the base stage?
Given this Dockerfile snippet, why does the prod stage fail with "file not found" error? FROM alpine AS base RUN echo "Hello" > /greeting.txt FROM alpine AS prod COPY /greeting.txt /greeting.txt CMD cat /greeting.txt
ACOPY command in prod stage tries to copy from local build context, not from base stage
BFile /greeting.txt does not exist in base stage
CCMD syntax is incorrect and causes failure
DAlpine image does not support COPY command
Attempts:
2 left
💡 Hint
COPY without --from copies from build context, not from other stages.
🔀 Workflow
advanced
2:00remaining
Best workflow to build and push dev and prod images using multi-stage Dockerfile
You have a multi-stage Dockerfile with stages dev and prod. Which sequence of commands correctly builds and pushes both images tagged for dev and prod?
A
docker build --target dev -t myapp:dev .
docker build --target prod -t myapp:prod .
docker push myapp:dev
# forgot to push prod
B
docker build -t myapp:dev .
docker build -t myapp:prod .
docker push myapp:dev
docker push myapp:prod
C
docker build --target prod -t myapp:prod .
docker push myapp:prod
docker build --target dev -t myapp:dev .
docker push myapp:dev
D
docker build --target dev -t myapp:dev .
docker push myapp:dev
docker build --target prod -t myapp:prod .
docker push myapp:prod
Attempts:
2 left
💡 Hint
Build and push each target image separately with correct tags.
🧠 Conceptual
expert
2:00remaining
Why use multi-stage builds for different environments in Docker?
What is the main advantage of using multi-stage Docker builds with separate stages for dev and prod environments?
AIt forces all environment files to be included in every image, increasing consistency
BIt allows creating smaller, optimized images for each environment by sharing common layers
CIt makes the build process slower but easier to debug
DIt requires separate Dockerfiles for each environment, improving clarity
Attempts:
2 left
💡 Hint
Think about image size and reuse of common parts.

Practice

(1/5)
1. What is the main purpose of using multi-stage builds in Docker for different environments?
easy
A. To automatically update Docker images from the internet
B. To run multiple containers simultaneously
C. To create smaller and cleaner Docker images by separating build and runtime stages
D. To increase the size of the Docker image for better performance

Solution

  1. Step 1: Understand multi-stage build concept

    Multi-stage builds separate the build environment from the runtime environment to avoid including unnecessary build tools in the final image.
  2. Step 2: Identify the benefit for different environments

    This separation results in smaller, cleaner images optimized for the target environment, such as production.
  3. Final Answer:

    To create smaller and cleaner Docker images by separating build and runtime stages -> Option C
  4. Quick Check:

    Multi-stage builds = smaller images [OK]
Hint: Remember: multi-stage means build then runtime separate [OK]
Common Mistakes:
  • Thinking multi-stage runs multiple containers
  • Believing it automatically updates images
  • Assuming it increases image size
2. Which of the following is the correct syntax to name a build stage in a Dockerfile?
easy
A. STAGE node:18 build
B. FROM node:18 AS build
C. BUILD node:18 AS build
D. FROM node:18 WITH build

Solution

  1. Step 1: Recall Dockerfile stage naming syntax

    Docker uses the syntax 'FROM <image> AS ' to name a build stage.
  2. Step 2: Check each option

    Only FROM node:18 AS build uses the correct 'FROM ... AS ...' syntax; others are invalid Dockerfile commands.
  3. Final Answer:

    FROM node:18 AS build -> Option B
  4. Quick Check:

    Stage naming = FROM ... AS ... [OK]
Hint: Look for 'FROM ... AS ...' to name stages [OK]
Common Mistakes:
  • Using 'STAGE' or 'BUILD' keywords incorrectly
  • Forgetting the AS keyword
  • Using 'WITH' instead of AS
3. Given this Dockerfile snippet, what will be the size characteristic of the final image?
FROM node:18 AS build
RUN npm install
FROM node:18-slim
COPY --from=build /app/node_modules /app/node_modules
medium
A. The final image will be smaller because it only copies needed files from build stage
B. The final image will be the same size as the build stage
C. The final image will fail to build due to missing files
D. The final image will be large because it includes all build tools

Solution

  1. Step 1: Analyze the multi-stage build steps

    The first stage installs dependencies with full Node image; the second stage uses a smaller Node image and copies only node_modules from the build stage.
  2. Step 2: Understand image size impact

    Because only necessary files are copied, the final image is smaller than the full build environment image.
  3. Final Answer:

    The final image will be smaller because it only copies needed files from build stage -> Option A
  4. Quick Check:

    Copying from build stage reduces final image size [OK]
Hint: Copy only needed files from build stage to keep image small [OK]
Common Mistakes:
  • Assuming all build tools are included in final image
  • Thinking the build will fail due to missing files
  • Believing final image equals build stage size
4. Identify the error in this Dockerfile snippet for multi-stage build:
FROM python:3.12 AS build
RUN pip install -r requirements.txt
FROM python:3.12-slim
COPY --from=builder /app /app
medium
A. The COPY command syntax is incorrect
B. The requirements.txt file is missing
C. The base images are incompatible
D. The stage name 'builder' does not match the first stage name

Solution

  1. Step 1: Check stage names consistency

    The first stage is named 'build' but COPY uses '--from=builder', which does not exist.
  2. Step 2: Verify COPY command correctness

    The COPY syntax is correct, but the source stage name is wrong, causing build failure.
  3. Final Answer:

    The stage name 'builder' does not match the first stage name -> Option D
  4. Quick Check:

    Stage names must match exactly in COPY --from= [OK]
Hint: Match stage names exactly in COPY --from= [OK]
Common Mistakes:
  • Using wrong stage name in COPY
  • Confusing syntax errors with naming errors
  • Assuming base images cause this error
5. You want to build a Docker image that uses a heavy build environment only during build, but produces a minimal runtime image for production. Which multi-stage Dockerfile snippet correctly achieves this?
hard
A. FROM golang:1.20 AS build WORKDIR /app RUN go build -o app . FROM alpine:latest COPY --from=build /app/app /app/app CMD ["/app/app"]
B. FROM alpine:latest AS build RUN apk add go WORKDIR /app RUN go build -o app . FROM golang:1.20 COPY --from=build /app/app /app/app CMD ["/app/app"]
C. FROM golang:1.20 WORKDIR /app RUN go build -o app . CMD ["/app/app"]
D. FROM alpine:latest RUN apk add go WORKDIR /app RUN go build -o app . CMD ["/app/app"]

Solution

  1. Step 1: Identify build and runtime separation

    FROM golang:1.20 AS build WORKDIR /app RUN go build -o app . FROM alpine:latest COPY --from=build /app/app /app/app CMD ["/app/app"] uses golang image for building, then copies only the built app to a minimal alpine image for runtime.
  2. Step 2: Check other options for correctness

    FROM alpine:latest AS build RUN apk add go WORKDIR /app RUN go build -o app . FROM golang:1.20 COPY --from=build /app/app /app/app CMD ["/app/app"] builds in alpine but runs in golang image, which is opposite of desired minimal runtime. Options C and D do not separate build and runtime stages.
  3. Final Answer:

    Option A correctly uses multi-stage build for minimal runtime image -> Option A
  4. Quick Check:

    Build heavy, run minimal = FROM golang:1.20 AS build WORKDIR /app RUN go build -o app . FROM alpine:latest COPY --from=build /app/app /app/app CMD ["/app/app"] [OK]
Hint: Build in heavy image, copy to minimal runtime image [OK]
Common Mistakes:
  • Building in minimal image causing slow builds
  • Running heavy image in production
  • Not separating build and runtime stages
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard