0
0
Dockerdevops~20 mins

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

Choose your learning style9 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.