Why do we use multi-stage builds in Dockerfiles when creating container images?
Think about how build tools and runtime dependencies differ.
Multi-stage builds allow you to use one stage to compile or build your application and then copy only the necessary artifacts to a smaller runtime image. This reduces the final image size and improves efficiency.
Given this Dockerfile snippet, what will be the size difference between the final image and the build stage image?
FROM python:3.12-slim AS build RUN pip install --no-cache-dir numpy FROM python:3.12-slim COPY --from=build /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages CMD ["python3"]
Consider what is copied from the build stage to the final stage.
The build stage installs numpy and any build dependencies, but only the installed packages are copied to the final image. Build tools and caches are excluded, making the final image smaller.
Which Dockerfile snippet correctly uses multi-stage builds to compile a Go application and produce a small final image?
Remember the build stage needs the Go compiler, but the final image should be minimal.
Option D uses golang image to build the app, then copies the compiled binary to a small alpine image. Option D reverses the stages causing build failure. Options C and D do not use multi-stage builds.
You created a multi-stage Dockerfile but the final image is missing some files needed at runtime. What is the most likely cause?
Think about how files move between stages in multi-stage builds.
If files are not explicitly copied from the build stage to the final stage, they won't be present in the final image. The COPY --from=build command is essential.
You want to optimize your CI pipeline to build and test a Node.js app using multi-stage Docker builds. Which workflow best reduces build time and image size?
Consider how multi-stage builds can separate testing and production environments.
Option B uses multi-stage builds to run tests in the build stage and copies only production files to the final image, reducing size and improving pipeline efficiency. Other options either increase image size or complicate the workflow.