Skip to content

COPY: failed to compute cache key … not found although the file exists

Containertested on Docker 29.9.0, buildx v0.38.0

COPY does not read from your folder but from the build context the client sends to BuildKit. Whatever the .dockerignore excludes does not exist for the build – even if ls shows the file.

Symptom

docker build -t notes-api:r09 notes-api
Dockerfile      Dockerfile.naiv go.mod          go.sum          main.go
 => ERROR [build 3/6] COPY go.mod go.sum ./                                    0.0s
ERROR: failed to build: failed to solve: failed to compute cache key: failed to
calculate checksum of ref …: "/go.sum": not found

The first line is the output of ls notes-api: go.sum is in the folder. The build still fails at COPY.

Diagnosis

A build check finds the error without building; the .dockerignore shows the cause.

cat notes-api/.dockerignore
docker build --check --build-arg BUILDKIT_DOCKERFILE_CHECK=experimental=all notes-api
# Allowlist: alles raus, nur der Quellcode rein
*
!*.go
!go.mod
WARNING: CopyIgnoredFile - https://docs.docker.com/go/dockerfile/rule/copy-ignored-…
Attempting to Copy file "go.sum" that is excluded by .dockerignore

Causes

*Allowlist without an exception for go.sum

In an allowlist, * excludes everything and every needed file needs its own ! line. Here !go.sum is missing.

last line winsOrder in the .dockerignore

The last matching line applies. A later pattern can cancel an earlier exception.

Fix

Add the missing exception and rebuild.
# Allowlist: alles raus, nur der Quellcode rein
*
!*.go
!go.mod
!go.sum
docker build -q -t notes-api:r09-fix notes-api

Don't: Delete the .dockerignore – then .git, .env and local artifacts end up in the context and possibly in the image.

▶
Break it yourself and find it – free
Dockerfiles hands-on, Day 15: diagnose and repair ten broken Dockerfiles against the clock.
Go to day 15 →

FAQ

Allowlist or denylist?

A denylist (exclude only what you know) is more robust. If you use an allowlist, run docker build --check in CI.

How do I see what is really in the context?

With a context spy – a throwaway Dockerfile that copies the whole context and lists it with find (Day 3).

Related error patterns

More