What is the main benefit of minimizing the number of layers in a Docker image?
Think about how Docker stores image data and how layers affect storage.
Each Dockerfile command creates a new layer. Fewer layers mean less overhead and smaller images, which speeds up builds and deployments.
What will be the size difference between these two Dockerfiles after building?
Dockerfile A:
FROM alpine RUN apk add curl RUN apk add git
Dockerfile B:
FROM alpine RUN apk add curl && apk add git
Consider how Docker layers are created for each RUN command.
Dockerfile A creates two layers for two RUN commands. Dockerfile B combines them into one RUN command, creating one layer, which reduces image size.
Which Dockerfile snippet correctly installs packages and cleans cache in a single layer to minimize image size?
Think about how to combine commands to avoid extra layers and clean cache in the same layer.
Option C combines update, install, and cleanup in one RUN command, creating a single layer and reducing image size. Options A and B create multiple layers. Option C uses semicolons which work but is less common and may cause issues in some shells.
A developer wrote this Dockerfile snippet:
RUN apt-get update RUN apt-get install -y curl RUN rm -rf /var/lib/apt/lists/*
Why does the final image size remain large despite cleaning the cache?
Think about how Docker layers store filesystem changes.
Each RUN command creates a new layer. Even if cache files are deleted in a later layer, they still exist in earlier layers, so the image size does not reduce.
Given these Dockerfile commands, which order minimizes rebuild time when only application code changes?
1. COPY package.json /app/ 2. RUN npm install 3. COPY . /app
Consider which files change frequently and how Docker caches layers.
Copying package.json and running npm install first caches dependencies. Then copying the rest of the code allows rebuilds without reinstalling packages if only app code changes.