Skip to content
COPY failed: file not found in build context

COPY failed: file not found in build context

DodaTech 2 min read

The error “COPY failed: file not found in build context” means Docker’s COPY instruction in your Dockerfile references a file or directory that does not exist relative to the build context.

What It Means

The COPY instruction in a Dockerfile copies files from the build context (the directory sent to the Docker daemon) into the image. If the source path does not exist within that context, Docker raises this error and the build fails.

Why It Happens

  • The source file path is absolute (e.g., /home/user/app/config.json) but COPY only accepts relative paths.
  • The file is outside the build context directory but COPY ../file . is not allowed.
  • The file was not committed to version control or was deleted before the build.
  • The .dockerignore file excludes the source file.
  • The source path is misspelled or the file extension is wrong.
  • The build context is a different directory than expected (e.g., running docker build from the wrong folder).

How to Fix It

1. Check that the file exists relative to the build context

ls -la ./relative/path/to/file

2. Ensure COPY uses a relative path

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

3. Move the file into the build context

mv /some/other/location/file.txt ./project/

4. Check .dockerignore for exclusions

cat .dockerignore

5. Build from the correct directory

docker build -t myimage .   # "." is the build context

6. Use a larger build context if needed

docker build -t myimage -f ./docker/Dockerfile .

FAQ

Can COPY access files outside the build context?
No. For security reasons, Docker does not allow COPY to reference files outside the build context. The build context is set by the last argument to docker build (usually .). If you need files from elsewhere, copy them into the context first.
What is the difference between COPY and ADD?
COPY is the recommended instruction for copying local files into an image. ADD can also copy remote URLs and auto-extract tar archives, but it has less transparent behavior. Use COPY unless you specifically need ADD’s extra features.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro