Docker
File Management
Parent Directory
Containerization
DevOps Tools

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:

bash
docker build -t my-image:latest -f path/to/your/Dockerfile path/to/context

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.

A common suggestion is to symlink the outside file into the context:

bash
1ln -s ../path/to/parent/file.txt your-context-dir/
2
3# Dockerfile
4COPY file.txt /app/

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:

dockerfile
1# Start by using a base image that makes it easy to copy the required files
2FROM alpine as builder
3COPY path/from/parent/dir/file.txt /file.txt
4
5# Final stage
6FROM your-base-image
7COPY --from=builder /file.txt /app/

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.
TechniqueDescriptionProsCons
Build Context ChangeAlter the build context to include parent folder.Simple to implement.Can unintentionally increase context.
Symbolic LinksCreate symbolic link within context pointing to parent file.No need to change context directory.Links pointing outside the context are not copied.
Multi-stage BuildsUse a temporary stage to handle external files.Better separation and flexibility.Source files must still be in the context.
Best PracticesOrganize 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.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design