ERROR: failed to build: failed to solve: failed to read dockerfile: open Dockerfile:
no such file or directory means there is no file named exactly
Dockerfile in the build context folder. Either you ran the build from the
wrong folder, or Windows saved the file as Dockerfile.txt.
Point the build at the folder that holds the file, rename the file to
Dockerfile with no extension, or name it explicitly with
-f. Two of these were tested below.
The error
PS> docker build -t fix8-cli-img:7 .
#0 building with "desktop-linux" instance using docker driver
#1 [internal] load build definition from Dockerfile
#1 transferring dockerfile: 2B done
#1 DONE 0.1s
ERROR: failed to build: failed to solve: failed to read dockerfile: open Dockerfile: no such file or directory
This is the run inside the app folder; the run from its parent printed the
same last line. The tell is transferring dockerfile: 2B: BuildKit sent two
bytes, not a file.
Why it happens
docker build . means "use this folder as the context and read the file
Dockerfile at its root". The name is matched exactly. In the test the file
lived one folder down, in app, and was called Dockerfile.txt,
which is what an editor that adds .txt by default can leave behind. Explorer hides
known extensions by default, so the file looks like Dockerfile while its
real name is not.
Running the same command inside app failed the same way, which proves the
extension alone is enough to trigger the error. A dir in the folder shows
the real name.
The fix
Name the file with -f and give the folder as the context:
PS> docker build -t fix8-cli-img:7 -f app\Dockerfile.txt app
#1 [internal] load build definition from Dockerfile.txt
#1 transferring dockerfile: 107B done
Or rename it and build the folder:
PS> Rename-Item app\Dockerfile.txt Dockerfile
PS> docker build -t fix8-cli-img:7 app
#1 [internal] load build definition from Dockerfile
#1 transferring dockerfile: 103B done
PS> docker run --rm --name fix8-cli-r fix8-cli-img:7
built
Renaming is the better fix, because every tool, Compose included, looks for
Dockerfile by default. Keep -f for projects that hold several
Dockerfiles on purpose. Note that the last argument of docker build is the
context folder, not the Dockerfile: COPY paths are resolved against it.
How it was reproduced
Docker Desktop on Windows 11, client and engine 29.9.2 with BuildKit on the
desktop-linux builder, Windows PowerShell 5.1, the cached
alpine:latest image. A three-line Dockerfile saved as
app\Dockerfile.txt; docker build . from the parent folder and
from app both gave the error. Both fixes built the image, which printed
built. PowerShell shows the blank lines of the build log as
System.Management.Automation.RemoteException; they are shown empty here.
The image name is the test's own.
Frequently asked
- Why does docker build say open Dockerfile: no such file or directory when the file exists?
- The file is not named exactly Dockerfile in the context folder. On Windows it is often Dockerfile.txt with the extension hidden, or the build runs from a different folder. Run dir to see the real name.
- How do I build with a Dockerfile that has a different name?
- Pass it with -f, for example docker build -f app\Dockerfile.txt app. The last argument stays the context folder.
- How do I remove the .txt extension from a Dockerfile on Windows?
- Run Rename-Item Dockerfile.txt Dockerfile in PowerShell, or turn on file name extensions in Explorer and rename it there.
More decoded errors in the Fixes category.