Dockerfile COPY from a parent directory
Codemia Knowledge Hub ·
COPY ../file.txt does not work, because a Dockerfile can only read files inside the build context. The fix is to make the parent directory the context: run docker build from there (or pass it as the context path) and use -f to point at the Dockerfile in its subdirectory.
How the build context limits COPY
When you run docker build, the Docker CLI sends the directory you pass as the context path (often .) to the Docker daemon. COPY and ADD can only see files inside that directory. Docker restricts builds to the context for security and performance: if builds could reach outside it, they might pull in unneeded files, inflate image sizes or expose sensitive data.
Making the parent directory the build context
Pass a context path that contains both the Dockerfile and the files you need, and point -f at the Dockerfile:
Paths in COPY are then relative to that context, so a file from the parent directory is referenced by its path from the new context root. A larger context can slow builds, so exclude unneeded files with .dockerignore.
Why symbolic links do not get around it
A common suggestion is to symlink the outside file into the context:
This does not work as hoped. The build context is sent with the link itself, not the file it points to, so a link that resolves outside the context cannot be copied. Copy the file into the context (for example in a build script) instead of linking it.
Multi-stage builds still read from the context
Multi-stage builds let you prepare files in a temporary stage and copy only the results into the final image:
Every COPY from the host, including the one in the builder stage, still reads from the build context. This pattern helps keep the final image small, but path/from/parent/dir/file.txt must be inside the context for it to work.
Keeping projects buildable
- Minimize context size: ignore unneeded files with
.dockerignore. - Security: think about what you expose before widening the context.
- File organization: group resources so the Dockerfile's directory, or one directory above it, contains everything the build needs.
| Technique | Description | Pros | Cons |
| Build Context Change | Alter the build context to include parent folder. | Simple to implement. | Can unintentionally increase context. |
| Symbolic Links | Create symbolic link within context pointing to parent file. | No need to change context directory. | Links pointing outside the context are not copied. |
| Multi-stage Builds | Use a temporary stage to handle external files. | Better separation and flexibility. | Source files must still be in the context. |
| Best Practices | Organize project architecture logically, minimize context, consider security. | Enhances efficiency and security. | Requires careful planning. |
Of these, only changing the build context actually brings a parent directory's files into the build; the symlink and multi-stage rows need the file to be inside the context already.
Related reading
- Docker and securing passwords
- Docker app server ip address 127.0.0.1 difference of 0.0.0.0 ip
- Docker Apple Silicon/M1 Preview MySQL no matching manifest for linux/arm64/v8 in the manifest list entries
- Docker build fails due to kafka undefined error
- docker build with --build-arg with multiple arguments
- Docker complains about no space left on device, how to clean up?
- docker build requires 1 argument. See 'docker build --help
- Docker build taking too long when installing grpcio via pip
System design interview problems on this topic
- Design a Container Orchestration System: solution and walkthrough
- Design a Code Deployment System: solution and walkthrough
- Design GitHub Actions: solution and walkthrough
- Design a Serverless Architecture Framework: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.