ERROR: failed to build: failed to solve: the Dockerfile cannot be empty came
from a Dockerfile of zero bytes. Open the file docker build read, check that the content you
expect is saved in it, and give it its instructions; the fixed test file held
FROM alpine:latest and CMD ["sh"].
In the test a Dockerfile of length 0 failed, and the same build with those two
lines in the file ended with exit code 0. A file that is not empty but has no instructions
gave another message, shown below.
The error
(Get-Item "$lab\empty\Dockerfile").Length
0
docker build -t fix12-dkbuild-empty . 2>&1
#0 building with "desktop-linux" instance using docker driver
#1 [internal] load build definition from Dockerfile
#1 transferring dockerfile: 31B 0.0s done
#1 DONE 0.0s
ERROR: failed to build: failed to solve: the Dockerfile cannot be empty
exit=1
The first block is PowerShell: the length of the file, 0. The second is the build, run in
cmd. The 31B on the transfer line was printed for this zero-byte
file.
Why it happens
The build found a file named Dockerfile in the folder, but the file had
nothing in it. The build stopped right after loading the definition, step
#1, before any image lookup.
In these runs the message came from the zero-byte file only. A file of three line breaks,
3 bytes, failed with file with no instructions instead, and so did a file of
two comment lines. So here "cannot be empty" pointed at the file on disk having no bytes,
not at its syntax.
The fix
FROM alpine:latest
CMD ["sh"]
docker build --progress=plain -t fix12-dkbuild-emptyfix . 2>&1
...
#4 [1/1] FROM docker.io/library/alpine:latest@sha256:294b683cb724975bec92580e1e685676bd4b50bda910ddb8c51d4cabeaec77e6
...
#5 naming to docker.io/library/fix12-dkbuild-emptyfix:latest done
#5 unpacking to docker.io/library/fix12-dkbuild-emptyfix:latest 0.0s done
#5 DONE 0.2s
exit=0
The file above is the whole Dockerfile of the fixed run. Checking the length of the old file first, as in the error block, tells you whether the file on disk is the empty one.
How it was reproduced
A zero-byte Dockerfile made with PowerShell's
New-Item -ItemType File in a scratch folder, built on Docker Desktop 4.99.0 for
Windows with client and engine 29.9.3 and buildx v0.38.1; the fix used the cached
alpine:latest (image id 294b683cb724). Build commands ran in
cmd, started from PowerShell as cmd /c "... 2>&1", with the
exit code printed after each; $lab is the test's scratch folder. Five builds:
the empty file twice (with and without --progress=plain), the file of line
breaks, the fixed file, and a file of two comment lines whose capture is copied, marked as
a copy, from the evidence of the
file with no instructions
post. The folders lab\empty, lab\blank, lab\emptyfix
and lab\comments and the image tags fix12-dkbuild- plus each
folder name are the test's own.
Frequently asked
- What does 'the Dockerfile cannot be empty' mean?
- In the test it came from a Dockerfile of zero bytes. Writing FROM alpine:latest and CMD ["sh"] into the file fixed the build.
- How do I check whether my Dockerfile is empty?
- Check its length on disk. In the test the PowerShell line (Get-Item "$lab\empty\Dockerfile").Length printed 0 for the file that failed in the test.
- Is a Dockerfile with only blank lines empty?
- Not to this message. A file of three line breaks failed with file with no instructions instead of the Dockerfile cannot be empty.
More decoded errors in the Fixes category. A file of only comments is covered in file with no instructions, and a missing file in failed to read dockerfile.