docker build stops at COPY because the file is not in the build context
COPY failed: stat /var/lib/docker/tmp/docker-xxx: no such file or directoryShort 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.
Fix it fast
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 .2Match what you see, then jump to its fix
- 1
The 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
The file is under the context folder, yet the build says it is not found.A .dockerignore pattern removes the file from the contextFix - 3
The 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
- 1
What does the error look like?
COPY failed: stat /var/lib/docker/tmp/docker-xxx: no such file or directoryERROR: 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
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 .dockerignore2Fix
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
.git3Verify
Rebuild; the COPY step now finds the file.
docker build -t my-app .Confirmed by docs.docker.com
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" Dockerfile2Fix
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.jar3Verify
Build the image and see the final stage complete.
docker build -t my-app .Confirmed by docs.docker.com
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
Servers
The Java SDK finds no AWS credentials on an EC2 instance or in a container on it
InstanceProfileCredentialsProvider()
The SDK walks a fixed list of credential sources and the EC2 instance role is the last one.
3 causes, each with a fixServers
Docker build fails to resolve the openjdk slim-buster base image
failed to resolve source metadata for docker.io/library/openjdk:21-slim-buster
The tag you named does not exist any more.
2 causes, each with a fixServers
A Java container dies at start with 'insufficient memory' or errno=12
os::commit_memory failed; error='Not enough space' (errno=12)
The JVM asked the operating system for more memory than the container is allowed to use.
3 causes, each with a fixServers
MongoDB says Authentication failed from a Java app against a Docker container
MongoCommandException: Command failed with error 18 (AuthenticationFailed): 'Authentication failed.' on server localhost:27017
Error 18 means MongoDB found no matching user in the database it checked.
4 causes, each with a fixServers
Testcontainers fails with Could not connect to Ryuk at localhost
docker ps shows a testcontainers/ryuk container while the test fails
Ryuk is the helper container Testcontainers starts to clean up after tests.
3 causes, each with a fixServers
Testcontainers (Java) says it could not find a valid Docker environment on a Mac
Client version 1.32 is too old. Minimum supported API version is 1.44
Testcontainers tried every way it knows to reach Docker and none answered.
3 causes, each with a fix