Docker: How to Add a File from a Parent Directory

When building Docker images, you may want to include a file that is outside the current build context—such as one from a parent directory. However, Docker has strict rules about what can be accessed during a build.

In this blog post, you’ll learn:

  • ❓ Why Docker doesn’t allow access to parent folders by default
  • 🔄 Workarounds to include files from outside the build context
  • 🧪 Practical examples
  • 🔐 Best practices for security and portability

❗ Problem: Can’t Access Parent Directory Files

Let’s say your folder structure looks like this:

project/
├── Docker/
│   └── Dockerfile
├── config/
│   └── settings.json

You want to copy ../config/settings.json into your Docker image. You try this in your Dockerfile:

COPY ../config/settings.json /app/settings.json

But it fails with an error like:

COPY failed: Forbidden path outside the build context: ../config/settings.json

🔒 Why This Happens

Docker’s COPY and ADD instructions only work with files inside the build context. This is a security feature to prevent unintended access to files on your system during image builds.


✅ Solution: Change the Build Context

The most straightforward fix is to set the build context at a higher level using the docker build command.

✅ Example:

Instead of running this (from Docker/):

docker build -t my-app .

Run it from the project root:

docker build -f Docker/Dockerfile -t my-app .

Now your COPY will work:

COPY config/settings.json /app/settings.json

Why this works: You’re telling Docker the build context is the full project/ directory, so it can access config/.


📁 Alternative: Move Files Temporarily

If changing the build context is not an option, another workaround is to temporarily copy the file into the build directory before building:

Example Bash Script:

cp ../config/settings.json ./Docker/
docker build -t my-app ./Docker
rm ./Docker/settings.json

⚠️ Be sure to clean up after the build to avoid accidental file inclusion or confusion.


🧠 Tip: Use .dockerignore Wisely

If you change the context to the root directory, make sure to use a .dockerignore file to avoid sending unnecessary files to Docker.

Example .dockerignore:

*.log
node_modules
.git
.env

✅ This reduces build time and keeps your image clean.


🔐 Best Practices

TipWhy It Matters
Never copy sensitive files from outsidePrevents security risks
Keep Dockerfile in project root if possibleSimplifies builds
Use .dockerignore to control contextImproves performance and security
Keep Docker contexts consistent in teamsAvoids “works on my machine” issues

🧠 Summary

GoalRecommended Approach
Copy file from parent directorySet build context to project root
Prevent extra files from copyingUse .dockerignore
Can’t change contextCopy files manually before build

🏁 Final Thoughts

Docker doesn’t allow accessing files outside the build context for good reason: security and consistency. The most robust solution is to structure your project so that your Dockerfile has access to everything it needs—within the context.

With a properly configured build context and .dockerignore, you’ll avoid common errors and build more portable, secure images.

Sharing Is Caring:

Leave a Comment