[{"id":"dockeradvanced-001","courseId":"docker-advanced","domain":"images-builds","topic":"Multi-stage builds and unused stages","prompt":"A Dockerfile has stages base, stage1 and stage2, where stage1 and stage2 both start FROM base. With BuildKit enabled, you run docker build --target stage2 . What happens to stage1?","hint":"Ask which stages the target stage needs in order to be built.","explain":"A Dockerfile can hold several named stages, and a build can aim at one of them as its target. Think about which stages the target actually depends on, and which stages it can reach through FROM.","source":"https://docs.docker.com/build/building/multi-stage/","interaction":{"type":"single"},"options":[{"id":"a","text":"BuildKit skips it, because the stage2 target does not depend on it.","why":"BuildKit builds only the stages the target depends on. stage2 starts from base, so stage1 is not processed."},{"id":"b","text":"It is built, because every stage before the target runs in order.","whyNot":"That is the legacy builder's behavior. BuildKit processes only the stages the target depends on."},{"id":"c","text":"It is built, and its files are copied into the stage2 image automatically.","whyNot":"A stage contributes to another only through an explicit reference such as COPY --from or a FROM that names it. Nothing refers to stage1 here."},{"id":"d","text":"The build fails, because every stage must be used by the target.","whyNot":"An unused stage is simply skipped; it does not stop the build."}],"answer":"a","explanation":"BuildKit builds only the stages that the target stage depends on. Stages reach other stages only through references such as FROM or COPY --from.","concept":"BuildKit builds only the stages that the target stage depends on. Stages reach other stages only through references such as FROM or COPY --from.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","code":"FROM ubuntu AS base\nRUN echo \"base\"\n\nFROM base AS stage1\nRUN echo \"stage1\"\n\nFROM base AS stage2\nRUN echo \"stage2\"","takeaways":["With BuildKit, a stage the target does not depend on is skipped."]},{"id":"dockeradvanced-002","courseId":"docker-advanced","domain":"images-builds","topic":"Build cache invalidation","prompt":"You rebuild an image from an existing Dockerfile. Which TWO of these changes force Docker to rebuild a layer instead of reusing its cache?","hint":"Think about what a layer's cache depends on, and what a change does to the layers after it.","explain":"Docker reuses a cached layer only when nothing it depends on has changed. Consider what a COPY layer depends on, and what happens to later layers when one layer changes.","source":"https://docs.docker.com/build/cache/","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"You edit the contents of a file that a COPY instruction copies into the image.","why":"A COPY layer's cache depends on the files it copies, so an edited file invalidates that layer."},{"id":"b","text":"You change a RUN line near the top of the Dockerfile, so every layer below it rebuilds.","why":"When a layer changes, every layer after it is affected and has to be rebuilt."},{"id":"c","text":"You rerun the build with no edits, and the RUN apt-get update line runs again.","whyNot":"The cache for RUN instructions isn't invalidated automatically between builds, so the old layer is reused."},{"id":"d","text":"You touch a copied file so that only its modification time changes.","whyNot":"The modification time is not taken into account when the cache checksum is calculated."},{"id":"e","text":"You change one RUN line, so only that line rebuilds and later steps keep their cache.","whyNot":"Once the cache is invalidated, all subsequent Dockerfile commands generate new images and the cache isn't used."},{"id":"f","text":"You edit a file in the build context that no COPY or ADD instruction refers to.","whyNot":"Cache checks look at the files involved in COPY or ADD, so an unused file cannot invalidate a layer."}],"answer":"ab","explanation":"A cache hit requires the layer and its inputs to be unchanged. A changed COPY source or a changed layer invalidates that layer and every layer after it.","concept":"A cache hit requires the layer and its inputs to be unchanged. A changed COPY source or a changed layer invalidates that layer and every layer after it.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["A rebuild reuses cached RUN layers, so a change to an earlier layer is what reruns everything below it."]},{"id":"dockeradvanced-003","courseId":"docker-advanced","domain":"images-builds","topic":"ENV values in the built image","prompt":"A value set with ENV in a Dockerfile is still set when a container is started from the image built from it.","hint":"Consider whether ENV values belong to the image or only to the build.","explain":"Some Dockerfile instructions set values that are kept with the image. Consider where a value set during the build can still be seen once a container runs.","source":"https://docs.docker.com/reference/dockerfile/#env","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","why":"ENV values are kept with the resulting image and persist when a container is run from it."},{"id":"b","text":"False","whyNot":"Wrong: ENV values persist when a container is run from the resulting image."}],"answer":"a","explanation":"ENV sets environment variables that are stored in the image and persist into containers started from it. ARG values differ: they are not embedded in the image.","concept":"ENV sets environment variables that are stored in the image and persist into containers started from it. ARG values differ: they are not embedded in the image.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["Use ARG for build-only values; ENV values are visible in every container started from the image."]},{"id":"dockeradvanced-004","courseId":"docker-advanced","domain":"images-builds","topic":"ARG scope before its declaration","prompt":"A Dockerfile has USER ${username:-some_user} on line 2, then declares ARG username on line 3. You run docker build --build-arg username=what_user . Which user does the line-2 USER instruction get?","hint":"Think about which lines can see an ARG, counting from the line where it is declared.","explain":"An ARG variable takes effect from the line where it is declared, not from the top of the file. Think about what a variable reference gets when it appears before the line that declares it.","source":"https://docs.docker.com/reference/dockerfile/#arg","interaction":{"type":"single"},"options":[{"id":"a","text":"some_user, the fallback, because username is not declared yet on that line.","why":"The ARG is declared on line 3, so line 2 uses the ${username:-some_user} fallback."},{"id":"b","text":"what_user, the value passed with --build-arg.","whyNot":"The --build-arg value is available only from the ARG declaration on line 3 onward."},{"id":"c","text":"An empty string, because the variable is undefined on that line.","whyNot":"The :- form substitutes the fallback when the variable is unset, so the result is not empty."},{"id":"d","text":"The build fails, because username is used before it is declared.","whyNot":"The line does not fail; it evaluates to the fallback, and the build continues."}],"answer":"a","explanation":"An ARG takes effect from the line where it is declared. Before that line, a reference to the variable uses its fallback, or an empty string if there is none.","concept":"An ARG takes effect from the line where it is declared. Before that line, a reference to the variable uses its fallback, or an empty string if there is none.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","code":"FROM busybox\nUSER ${username:-some_user}\nARG username\nUSER $username","takeaways":["An ARG is visible only from the line that declares it onward."]},{"id":"dockeradvanced-005","courseId":"docker-advanced","domain":"images-builds","topic":"ONBUILD timing","prompt":"An ONBUILD instruction runs during the build of the image that contains it, before that image is saved.","hint":"Ask which build is meant to run a stored instruction.","explain":"Some instructions are saved into an image rather than run when that image is built. Think about when a saved instruction is carried out, and by which build.","source":"https://docs.docker.com/reference/dockerfile/#onbuild","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","whyNot":"ONBUILD doesn't otherwise affect the current build; it runs later, when a build uses this image as its base."},{"id":"b","text":"False","why":"Right: the trigger is stored in the image and runs only when a later build uses that image as the base."}],"answer":"b","explanation":"ONBUILD triggers are stored in the image and executed when it is used as the base for another build. They are not run during the image's own build.","concept":"ONBUILD triggers are stored in the image and executed when it is used as the base for another build. They are not run during the image's own build.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["ONBUILD is for base images: its triggers run in the child build, not the build that declares them."]}]