What is the main reason to use multi-stage builds when creating Docker images?
Think about how build tools and runtime requirements differ.
Multi-stage builds let you copy only the necessary artifacts from a build stage to the final image, leaving behind bulky build tools and dependencies. This reduces the final image size.
What will be the effect on the Docker image size after running RUN apt-get clean && rm -rf /var/lib/apt/lists/* in a Dockerfile?
FROM ubuntu:22.04 RUN apt-get update && apt-get install -y curl \ && apt-get clean && rm -rf /var/lib/apt/lists/*
Consider where package cache files are stored and if they remain in the image layers.
Cleaning the apt cache and removing package lists deletes unnecessary files from the image layers, reducing the final image size.
Which base image choice will generally produce the smallest Docker image for a Node.js app?
Think about minimal Linux distributions optimized for small size.
Alpine Linux is a minimal distribution designed for small size, so node:alpine images are much smaller than full Ubuntu or Debian base images.
You added cleanup commands to your Dockerfile to remove temporary files after installing packages, but the final image size is still large. What is the most likely reason?
Think about how Docker layers work and when files get saved.
Docker creates a new image layer after each RUN command. If cleanup is done in a separate RUN, the files exist in previous layers and still increase image size. Combining install and cleanup in one RUN keeps the image smaller.
You have a Dockerfile that compiles a Go application and then copies the binary to a minimal image. Which multi-stage Dockerfile snippet correctly produces the smallest final image?
FROM golang:1.20 AS builder WORKDIR /app COPY . . RUN go build -o myapp FROM scratch COPY --from=builder /app/myapp /myapp CMD ["/myapp"]
Consider the smallest base image possible and copying only what is needed.
Using scratch as the final base image and copying only the compiled binary results in the smallest image. Other options include unnecessary files or larger base images.