Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

COPY cannot see a file that sits in another folder

Error"/main.c": not found

Short answer

COPY and ADD only read files inside the build context, the folder you pass to docker build. A file in a sibling or parent folder is invisible to it, even if the Dockerfile is next to you. Move the context up, add a named context with --build-context, or check that .dockerignore is not hiding the file.

  • Applies to Docker Engine 28, Docker Buildx 0.29, BuildKit
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find out which folder you pass as the context. It is the last argument of docker build, not the folder of the Dockerfile. Print it, then list what is inside.

    # the last argument is the context
    docker build -f docker/Dockerfile .
    ls -la .
  2. 2Match what you see, then jump to its fix

    1. 1The path in COPY starts with ../ or points to a sibling folder, and the build reports that it is not foundThe file lives outside the context folderFix
    2. 2Dockerfile is in docker/ and the shared files are in the project root, but you build from docker/The context root is too deep: the file is above itFix
    3. 3The file is inside the context folder but COPY still reports it as not found.dockerignore removes the file before the build startsFix

What does the error look like?

  • "/main.c": not found

Cause 1 of 3

The file lives outside the context folder

1Check

Compare the path of the file with the context folder. If the file is not under the context, the builder never receives it.

pwd
ls ../config/

2Fix

Add the other folder as a named context, then copy from it with COPY --from. The first line enables the Dockerfile syntax that supports this.

# build command
docker build --build-context config=../config .

# Dockerfile
# syntax=docker/dockerfile:1
FROM nginx:alpine
COPY --from=config nginx.conf /etc/nginx/nginx.conf

3Verify

The build finishes and the file is in the image.

docker run --rm <image> ls -l /etc/nginx/nginx.conf

Confirmed by docs.docker.com

Did this fix it?

Cause 2 of 3

The context root is too deep: the file is above it

1Check

Check where docker build is run. If you run it inside the Dockerfile's folder, only that folder is the context.

pwd && docker build -f Dockerfile .

2Fix

Run the build from the folder that holds everything you need, and point -f at the Dockerfile. Write COPY paths relative to that root.

cd project-root
docker build -f docker/Dockerfile -t my-app .
# Dockerfile: COPY config/app.conf /etc/app.conf

3Verify

The same COPY line now finds the file.

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

Confirmed by docs.docker.com

Did this fix it?

Cause 3 of 3

.dockerignore removes the file before the build starts

1Check

The client removes every path matching .dockerignore before it sends the context, so COPY and ADD cannot reach those files. Read the file at the context root. The last matching line wins.

cat .dockerignore

2Fix

Delete the matching line, or re-include the file with a ! line placed after the pattern that excludes it.

# .dockerignore
*.md
!README.md

3Verify

Rebuild; the COPY step passes.

docker build -t my-app .

Confirmed by docs.docker.com

Did this fix it?

How do I stop it coming back?

  • Decide the context root first, then write every COPY path relative to it.
  • Keep one .dockerignore at the context root and review it when a file goes missing.
  • Use --build-context for shared files instead of copying them into each project.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.