Explore core concepts of Docker images and layers with scenario-based questions. This quiz assesses your understanding of image structure, layer management, optimization techniques, and common pitfalls in containerization workflows.
What happens if a Dockerfile instruction near the beginning of a build is changed before rebuilding the image?
Explanation: When an instruction is modified in a Dockerfile, all subsequent layers built on top of it will be invalidated and need to be rebuilt. This ensures consistency and reliability in the image. The option about only rebuilding the changed and final layer is incorrect because all following layers are affected. If caching is enabled, it only helps when instructions haven't changed, so saying no layers are rebuilt is incorrect. It's also misleading to say only the new instruction is rebuilt, as dependent layers must be recreated as well.
Why is it beneficial to place instructions that change infrequently near the top of a Dockerfile?
Explanation: Placing infrequently changing instructions near the top maximizes the effectiveness of the build cache, so these layers do not need to be rebuilt every time. Image pull speed is generally not affected by instruction order but by overall image size. The image size itself is determined by content, not instruction ordering. Layer deletion is unrelated to Dockerfile instruction placement.
If two images share a common base layer but have different application code, how does this affect disk space usage?
Explanation: Docker stores image layers using content-based hashes, so any layer that is identical between images will be stored only once to optimize disk usage. It's incorrect that each image duplicates all layers; only unique layers are duplicated. The idea that only the larger image keeps the base layer is incorrect, as all images reference the shared layer. Rebuilds only create new copies for altered layers, not for layers unchanged.
Which strategy helps to minimize the number of layers in a Docker image without affecting the final result?
Explanation: By chaining multiple commands into one RUN instruction, you reduce the number of intermediate layers created, making the image more efficient. Adding extra COPY instructions actually increases the number of layers and may lead to redundancy. Splitting each command into its own RUN creates unnecessary layers. Duplicating the base image setup is inefficient and increases image size unnecessarily.
Which Dockerfile instruction most often leads to creating a new layer that significantly increases image size if misused?
Explanation: COPY adds files or directories from the build context to the image, and unnecessary or large files can greatly increase the image size if not managed carefully. CMD only sets default commands for the container, not adding data, so it doesn't affect size. EXPOSE simply indicates which ports should be accessible, without impacting size. WORKDIR sets the working directory and doesn't store files, so it has minimal impact on image size.