docker compose build cannot find or read the Dockerfile
failed to read dockerfileShort answer
Compose looks for the Dockerfile in the wrong place. The build context is resolved from the folder of the Compose file, and a relative dockerfile path is resolved from that context, not from the Compose file. Put the path pair right, or name the file with -f when you build with the plain docker CLI.
Fix it fast
1Find out which cause you have
List the folder the Compose file sits in and the folder you named as the build context. The Dockerfile has to be in the context, or the dockerfile value has to lead to it from the context.
ls -la grep -n -A3 "build:" compose.yaml2Match what you see, then jump to its fix
- 1
The service sets context to a sub folder and dockerfile to a path that already starts at the Compose file's folderdockerfile is written relative to the Compose file, not to the contextFix - 2
build.context is missing or wrong, so it defaults to the project directoryThe context points at a folder that has no DockerfileFix - 3
docker build . fails while the file exists under a different name or extensionThe Dockerfile has another name and the plain docker build does not know itFix
- 1
What does the error look like?
failed to read dockerfiledocker compose up or docker compose build stops before the first build stepthe build works from one folder and fails from another
Cause 1 of 3
dockerfile is written relative to the Compose file, not to the context
1Check
Compose resolves a relative dockerfile path from the build context. With context: backend, a value of backend/Dockerfile is searched as backend/backend/Dockerfile.
grep -n -B1 -A4 "build:" compose.yaml
ls backend2Fix
Write the dockerfile path as seen from the context folder. To use a Dockerfile that lives outside the context, walk back up with ..
services:
api:
build:
context: backend
dockerfile: Dockerfile
worker:
build:
context: backend
dockerfile: ../backend.Dockerfile3Verify
Run the build again; the first step should now start.
docker compose build apiConfirmed by docs.docker.com
Cause 2 of 3
The context points at a folder that has no Dockerfile
1Check
A relative context is read from the directory that contains the Compose file. If context is not set, it is the project directory itself. Check which folder you run compose from.
pwd
ls Dockerfile2Fix
Set context explicitly to the folder that holds the Dockerfile and the files it copies, or run the command from the project directory.
services:
api:
build:
context: ./services/api3Verify
Build from the project directory.
docker compose buildConfirmed by docs.docker.com
Cause 3 of 3
The Dockerfile has another name and the plain docker build does not know it
1Check
docker build uses the -f (or --file) option to choose the Dockerfile. Without it the builder looks for the default name in the context. A file saved as Dockerfile.txt or with different letter case is not found on Linux.
ls -la | grep -i dockerfile2Fix
Rename the file to Dockerfile, or point to it with -f and give the context separately.
docker build -f Dockerfile.dev -t my-app .3Verify
The build starts and prints its first step.
docker build -f Dockerfile.dev -t my-app .Confirmed by docs.docker.com
How do I stop it coming back?
- Keep one Dockerfile per context folder and name it Dockerfile, so no dockerfile key is needed.
- Write context and dockerfile as a pair and read them together: context from the Compose file, dockerfile from the context.
- Run compose from the project directory in scripts and CI.
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