COPY cannot see a file that sits in another folder
"/main.c": not foundShort 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.
Fix it fast
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 .2Match what you see, then jump to its fix
- 1
The 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
Dockerfile 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
The file is inside the context folder but COPY still reports it as not found.dockerignore removes the file before the build startsFix
- 1
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.conf3Verify
The build finishes and the file is in the image.
docker run --rm <image> ls -l /etc/nginx/nginx.confConfirmed by docs.docker.com
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.conf3Verify
The same COPY line now finds the file.
docker build -f docker/Dockerfile -t my-app .Confirmed by docs.docker.com
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 .dockerignore2Fix
Delete the matching line, or re-include the file with a ! line placed after the pattern that excludes it.
# .dockerignore
*.md
!README.md3Verify
Rebuild; the COPY step passes.
docker build -t my-app .Confirmed by docs.docker.com
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
Servers
apk cannot find py-pip in an Alpine Docker image
py-pip (no such package):
On current Alpine releases there is no package called py-pip.
2 causes, each with a fixServers
ng serve runs in the container but localhost:4200 does not open
The browser cannot open localhost:4200 although ng serve is running in the container
ng serve listens on localhost by default, which inside a container is the container itself.
3 causes, each with a fixServers
Angular routes return 404 from nginx when you refresh or open a link
The home page loads, but refreshing /some/route shows an nginx 404
Angular routes like /users/42 are not files, so nginx looks for them on disk and returns 404.
3 causes, each with a fixServers
AH00558: Apache in Docker cannot reliably determine its fully qualified domain name
Set the 'ServerName' directive globally to suppress this message
AH00558 is a warning, not a crash.
2 causes, each with a fixServers
Apache in a Docker container logs AH01114 when it proxies to a backend
AH01114: HTTP: failed to make connection to backend: api
Apache's proxy worker could not open a connection to the address in ProxyPass.
5 causes, each with a fixServers
Apache logs SIGWINCH and exits on its own
the httpd container stops although nothing crashed
SIGWINCH is Apache's graceful-stop signal.
2 causes, each with a fix