Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

docker build stops at COPY because the file is not in the build context

ErrorCOPY failed: stat /var/lib/docker/tmp/docker-xxx: no such file or directory

Short answer

COPY can only read files that are inside the build context, the folder you pass to docker build. The file is missing there for one of three reasons: the context is the wrong folder, a .dockerignore pattern removed it, or, in a multi-stage build, the path does not exist inside the stage you copy from.

  • Applies to Docker Engine 27, Docker BuildKit, Java 21, Maven
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Run the build from the folder you think is the context and compare it with the source path in COPY. The last argument of docker build is the context, so a dot means the current folder.

    pwd
    ls -la target/*.jar
    docker build -t my-app .
  2. 2Match what you see, then jump to its fix

    1. 1The failing path is relative, for example target/app.jar, and the file exists on disk but not under the folder you gave to docker build.The context is a different folder than the one COPY expectsFix
    2. 2The file is under the context folder, yet the build says it is not found.A .dockerignore pattern removes the file from the contextFix
    3. 3The failure happens on a line with COPY --from=build in a multi-stage Dockerfile.COPY --from points at a path that does not exist in that build stageFix

What does the error look like?

  • COPY failed: stat /var/lib/docker/tmp/docker-xxx: no such file or directory
  • ERROR: failed to solve: failed to compute cache key:

Cause 1 of 3

The context is a different folder than the one COPY expects

1Check

Source paths in COPY are read relative to the root of the context. A path that leaves the context with ../ does not escape it; the ../ part is dropped. Files that your build generates (such as a jar) must also exist before the build starts.

grep -n "COPY" Dockerfile
ls -la target/

2Fix

Run the build from the project root, or move the context up to a folder that contains everything and point at the Dockerfile with -f.

mvn -q package
docker build -f docker/Dockerfile -t my-app .

3Verify

The COPY step passes and the image builds.

docker build -f docker/Dockerfile -t my-app .

Confirmed by docs.docker.com

Did this fix it?

Cause 2 of 3

A .dockerignore pattern removes the file from the context

1Check

The client reads .dockerignore from the root of the context and removes every matching file before the build starts, so COPY never sees it. A broad pattern such as target or *.jar is a common culprit.

cat .dockerignore
ls -la .dockerignore

2Fix

Remove or narrow the pattern that matches the file. A Dockerfile-specific ignore file named after the Dockerfile (for example build.Dockerfile.dockerignore) takes precedence over the general one, so check for that too.

# .dockerignore: do not exclude what the Dockerfile copies
node_modules
.git

3Verify

Rebuild; the COPY step now finds the file.

docker build -t my-app .

Confirmed by docs.docker.com

Did this fix it?

Cause 3 of 3

COPY --from points at a path that does not exist in that build stage

1Check

With --from, the source path is resolved from the root of the named stage's filesystem, not from your project folder. If the earlier stage wrote its output somewhere else (another WORKDIR, another target folder), the path is wrong.

grep -n -E "^FROM|WORKDIR|COPY --from" Dockerfile

2Fix

Use the absolute path where the stage really puts the file, and make sure the stage name matches the AS name on its FROM line.

FROM eclipse-temurin:21-jdk AS build
WORKDIR /src
COPY . .
RUN ./mvnw -q package

FROM eclipse-temurin:21-jre
COPY --from=build /src/target/app.jar /app/app.jar

3Verify

Build the image and see the final stage complete.

docker build -t my-app .

Confirmed by docs.docker.com

Did this fix it?

How do I stop it coming back?

  • Keep one Dockerfile at the project root and build with the dot as context, unless you have a reason to do otherwise.
  • Build the jar inside the Dockerfile in a build stage so the image does not depend on what exists on one machine.
  • Read .dockerignore whenever a new folder is added to the project.
  • Use absolute paths after COPY --from.

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.