Dockerfile COPY: package project files
Read the recipe
FROM busybox:1.37WORKDIR /appCOPY note.txt ./note.txtCMD ["cat", "note.txt"]The source note.txt belongs to the build context. The destination ./note.txt resolves to /app/note.txt because WORKDIR is /app. At runtime cat reads the packaged file. For ordinary local files, COPY is a clear way to declare exactly which inputs the image needs. With multiple sources use a directory destination ending in a slash. For the practice, keep the old tag and use a separate unused tag for the changed source. Compare outputs before cleanup; do not overwrite the baseline.
Try it in a local lab
Manual lab only. Use an approved disposable offline Linux VM with a local Docker Engine and a modern local Buildx builder using the docker driver. Check docker context show, docker version, docker buildx version and docker buildx ls before building. Verify the selected context and builder are local, and that reviewed busybox:1.37 is available to the builder and image store. Verify the base has no configured entrypoint and provides the utilities used in this recipe. Record its actual platform and digest; replace the tag with the reviewed digest for repeatability. No network egress is allowed in the VM: --network none limits RUN instructions, not all builder traffic. Do not enable network access if resolution fails. Check that the example image tag is unused and keep all data synthetic. In a dedicated lab shell, enter the indicated fixture directory and execute each command in order; stop on any unexpected failure. The fixture files are included in the archive or can be recreated from the snippets. Expected results are predictions, not tested outputs. Before contacting any Engine, resolve DOCKER_HOST, DOCKER_CONTEXT and CLI context overrides; also check BUILDX_BUILDER and any builder override. Confirm that the actual builder used by docker build is the approved local docker driver. Review the base configuration for inherited triggers, volumes, health checks, user, shell and required applets; stop if assumptions differ. Make a private working copy containing only the supplied synthetic files. An approved digest may require registry metadata resolution even when layers are present locally; if the offline builder cannot resolve it, stop and record the limitation. Record the actual base image ID, platform, builder version and output image ID; never substitute an unknown digest. Do not retry by enabling egress. Apply owner-approved time and resource budgets and retire the VM if cleanup is uncertain.
From the extracted archive root, open lab-fixtures/dockerfile-fundamentals/04-copy/. Save the Dockerfile exactly as shown. The following additional files, if any, belong beside it.
docker build --load --network none -t bm-v07-copy:lab .docker run --rm --pull=never --network none bm-v07-copy:labThe run should print packaged note. If you edit the host file later, an existing image still contains the old text; rebuild and start a new container to use the new content.
After all commands finish, check that the tag identifies only your lab image and no container depends on it. Remove the tag without forcing deletion. Build cache may remain until the disposable VM is retired. Record output and exit status before removing the exact lab tag. Recheck the tag against the recorded output image ID; a tag may have been reassigned.
Common mistake
COPY is not a bind mount. Do not copy secrets or the whole project without reviewing the context. A file removed by a later instruction may still be present in earlier layers.
Manual practice
Predict the output after editing only the lab copy of note.txt, then after rebuilding under a new unused tag. Explain the difference.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Dockerfile COPY: package project files — Knowledge check
Apply the lesson concepts to a new build or container scenario.
Where is ./note.txt copied in the example?
Does changing the source file on the host automatically update the already built image, without a new build?
Which instruction clearly selects a local source file?