COPY --from and artifact paths
A small example
FROM busybox:1.37 AS assembleWORKDIR /buildRUN mkdir /out && printf "version=1\n" > /out/info.txtFROM busybox:1.37WORKDIR /appCOPY --from=assemble /out/info.txt ./info.txtCMD ["cat", "/app/info.txt"]The source /out/info.txt is resolved from the root of assemble, not its WORKDIR. The destination ./info.txt is relative to /app in the receiving stage. Stage names describe intent and avoid fragile numeric references. COPY can also reference an image directly; a missing or mistyped stage name can be interpreted as an image reference and may trigger resolution. Only use declared stage names or reviewed offline image inputs. Check the stage name against the declarations before building. The lab forbids resolving unintended external images even if the build would succeed online.
Try it in a local lab
Manual lab only. Use an approved fresh disposable Linux VM with blocked network egress, a local Engine and local Buildx docker driver. Check docker context show, docker version, docker buildx version and docker buildx ls. Confirm reviewed preloaded native busybox:1.37 and, for the scratch example, golang:1.26; verify actual digest, root build user, default entrypoint, required applets/compiler and frontend support. Tags are mutable; record actual identities. Stop if any dependency needs fetching or builder is remote. --network none limits RUN networking, not all builder traffic. Confirm all bm-v10 tags are unused, including checks and full. Execute commands individually in the specified fixture directory and stop on unexpected failure. All expected results are predictions. No secrets, host mounts or registry pushes. Before querying or building, confirm DOCKER_HOST, DOCKER_CONTEXT and BUILDX_BUILDER overrides and the effective endpoint are approved; do not contact an unapproved endpoint. docker build normally uses the Engine default builder, not necessarily the starred Buildx builder. Record the effective local docker driver and platform; review the synthetic context and ignore rules. After each build, require exit status zero before running its tag; an earlier tag may still exist after failure.
Open lab-fixtures/docker-multi-stage-images/02-copy-from/ in the extracted archive. The Dockerfile and these files are supplied. Work on a disposable copy, not the original archive.
docker build --load --network none --progress=plain -t bm-v10-copy-from:lab .docker run --rm --pull=never --network none bm-v10-copy-from:labdocker run --rm --pull=never --network none bm-v10-copy-from:lab sh -c "test -f /app/info.txt && test ! -e /out/info.txt"Expect version=1 and a successful silent path check. The destination exists in /app; the original /out/info.txt is not copied to its original location.
Confirm the tag belongs to this lab and no containers depend on it. Remove only that tag without forcing. Cached data may remain; dispose of the VM rather than clearing a shared cache. Restore any edited fixture or discard the working copy.
Common mistake
Do not confuse COPY --from with COPY from the local context. Copy only the artifact needed, not a whole build filesystem.
Manual practice
On paper change the destination to /release/info.txt and update CMD. Explain which path changes and which source path remains unchanged.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
COPY --from and artifact paths — Knowledge check
Apply the multi-stage concept to a new scenario.
What is the source path relative to?
Where does ./info.txt land here?
Why use a named stage?