forbidden path outside the build context 🐳 Docker

COPY failed: forbidden path outside the build context: ../file

A COPY/ADD tries to read a file above the build context (../something); Docker only sends the context folder to the builder.

Seen on: Docker

Meaning

The build context is the directory passed to docker build. Anything outside it is invisible. Move the Dockerfile’s context up a level and use -f, or copy the files into the context first.

Common causes

  • COPY ../shared /app/shared in the Dockerfile
  • Monorepo: building a package from its own folder while it needs root files
  • Symlinks pointing outside the context

⚡ Quick fix

  1. Build from the parent: docker build -f app/Dockerfile .
  2. Compose: context: .. with dockerfile: app/Dockerfile
  3. Copy the files into the context in a pre-build step

Detailed fix by platform

Docker Compose

  1. bash
    services:
      api:
        build:
          context: ..
          dockerfile: api/Dockerfile

How to diagnose

  1. Context — Which folder is passed to build?
  2. Paths — Any ../ in COPY/ADD?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.