Docker Containerization Dockerfile and Image Building 2 — Questions and Answers
Question 1: What defines a multi-stage Docker build?
- Pushing the same image to multiple registries
- Using multiple FROM instructions in one Dockerfile to copy artifacts between stages and produce a lean final image (Correct answer)
- Building separate Dockerfiles in parallel on multiple machines
- Using multiple CMD instructions to run several processes
Correct answer: Using multiple FROM instructions in one Dockerfile to copy artifacts between stages and produce a lean final image
Multi-stage builds use multiple FROM instructions, allowing intermediate stages (e.g., a compiler image) to build artifacts that are then copied into a minimal final image, reducing image size.
Question 2: Which 'docker build' flag allows you to stop at a specific named stage in a multi-stage Dockerfile?
- --stage
- --target (Correct answer)
- --from
- --phase
Correct answer: --target
The --target flag tells Docker to build only up to and including the specified named stage, which is useful for debugging or producing intermediate artifacts.
Question 3: How does Docker layer caching improve build performance?
- Docker downloads layers in parallel from the registry
- Docker reuses cached layers from previous builds when Dockerfile instructions and their context have not changed (Correct answer)
- Docker stores only the final image layer and rebuilds intermediates
- Docker caches the build context but always re-executes instructions
Correct answer: Docker reuses cached layers from previous builds when Dockerfile instructions and their context have not changed
Docker caches each image layer; if an instruction and its inputs haven't changed since the last build, Docker reuses the cached layer and skips re-executing that step.
Question 4: Which base image is most commonly recommended for minimizing Docker image size in production?
- ubuntu:latest
- centos:7
- alpine:latest (Correct answer)
- debian:latest
Correct answer: alpine:latest
Alpine Linux is a minimal distribution (~5MB) that is widely used as a base image to keep Docker images small, reducing pull times and attack surface.
Question 5: What happens to Docker's build cache when a Dockerfile instruction changes?
- The entire cache is cleared for all images on the system
- The changed layer and all subsequent layers are invalidated and rebuilt (Correct answer)
- Only the changed layer is rebuilt; subsequent layers are reused
- The cache is unaffected and the old layer is reused
Correct answer: The changed layer and all subsequent layers are invalidated and rebuilt
Docker invalidates the cache starting at the changed instruction, so that changed layer and every layer after it are rebuilt to ensure correctness.
Question 6: What is the best practice for combining shell commands in a RUN instruction to reduce image layers?
- Use a separate RUN instruction for each command
- Chain commands with && in a single RUN instruction (Correct answer)
- Use the SHELL instruction before each command
- Place all commands in a CMD instruction instead
Correct answer: Chain commands with && in a single RUN instruction
Chaining commands with && in a single RUN instruction creates only one image layer, reducing overall image size compared to using multiple RUN instructions that each add a layer.
Question 7: In a multi-stage Dockerfile, how do you copy a file from a previous build stage into the current stage?
- COPY --from=<stage-name> <src> <dest> (Correct answer)
- ADD --stage=<stage-name> <src> <dest>
- RUN copy --stage <stage-name> <src> <dest>
- IMPORT --from=<stage-name> <src> <dest>
Correct answer: COPY --from=<stage-name> <src> <dest>
The COPY --from flag lets you reference a named or numbered previous build stage and copy files from it into the current stage, enabling the multi-stage pattern.
What defines a multi-stage Docker build?