Docker Community Forums

Share and learn in the Docker community.

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

I have a GitHub actions workflow to build a docker image:

name: Backend-Demo Docker Image CI
on:
  push:
    branches: [ master ]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Login to Azure Container Registry
        run: echo ${{ secrets.REGISTRY_PASSWORD }} | docker login ${{ secrets.LOGIN_SERVER_URL }} -u ${{ secrets.REGISTRY_USERNAME }} --password-stdin
      - name: Get the version
        id: vars
        run: echo ::set-output name=tag::$(echo ${GITHUB_REF:10})
      - name: Build the tagged Docker image
        run: docker build . --file backend/Dockerfile --tag backend-demo/spring-boot:v1.0

The Dockerfile is:

FROM openjdk:14-alpine
MAINTAINER example.com
RUN mkdir -p /opt/demo-0.0.1/lib
# Setting application source code working directory
WORKDIR /opt/demo-0.0.1/
COPY target/demo-0.0.1-SNAPSHOT.jar /opt/demo-0.0.1/lib/demo-0.0.1-SNAPSHOT.jar
# ADD target/demo-0.0.1-SNAPSHOT.jar /opt/demo-0.0.1/lib/

RUN sh -c 'touch demo-0.0.1-SNAPSHOT.jar'
ENTRYPOINT ["java"]
CMD ["-jar", "/opt/demo-0.0.1/lib/demo-0.0.1-SNAPSHOT.jar"]

But when I execute this workflow I got this error at the COPY instruction:

Step 5/8 : COPY target/demo-0.0.1-SNAPSHOT.jar /opt/demo-0.0.1/lib/demo-0.0.1-SNAPSHOT.jar
COPY failed: stat /var/lib/docker/tmp/docker-builder851513197/target/demo-0.0.1-SNAPSHOT.jar: no such file or directory
##[error]Process completed with exit code 1.

I have been checking and it looks a typical error when the file we have the Dockerfile in a different directory like my instruction:

docker build . --file backend/Dockerfile --tag backend-demo/spring-boot:v1.0

I also don’t have .dockerignore file and my Dockerfile is called Dockerfile precisely.

The target/demo-0.0.1-SNAPSHOT.jar file I am trying to copy is present in my github repository
Not sure what could be happening with the context, but probably this answer could be a good hint?

Hey mate,

In my experience this is because the Dockerfile and the resources that you need are not in the same build context or are NOT relative to the Dockerfile (in a folder underneath the Dockerfile) or have been excluded due to gitignore or dockerignore rules that are also in the same build context.

Try this,

Create Build Temp Area
Copy Docker File and ALL resources to Build Temp Folder

Your Temp structure may look something like

Build-Temp
- Dockerfile
- your.jar
- AppSrc folder

docker build /build_tmp/Dockerfile

Dockerfile

COPY your.jar /localtion
RUN something

COPY AppSrc /opt/myapplication

Everything must be relative to the Dockerfile (in my experience anyway)

Hope this helps a little

When you run

docker build . --file backend/Dockerfile …
The path argument . becomes the context directory. (Docker actually sends itself a copy of this directory tree, which is where the /var/lib/docker/tmp/… path comes from.) The source arguments of COPY and ADD instructions are relative to the context directory, not relative to the Dockerfile.

If your source tree looks like

.
±- backend
| – Dockerfile
– target
– demo-0.0.1-SNAPSHOT.jar
that matches the Dockerfile you show. But if instead you have

.
±- backend
±- Dockerfile
– target
– demo-0.0.1-SNAPSHOT.jar
you’ll get the error you see.

If you don’t need to refer to anything outside of the context directory, you can just change what directory you’re passing to docker build

COPY target/demo-0.0.1-SNAPSHOT.jar /opt/demo-0.0.1/lib/demo-0.0.1-SNAPSHOT.jar
docker build backend …
Or, if you do have other content you need to copy in, you need to change the COPY paths to be relative to the topmost directory.

COPY backend/target/demo-0.0.1-SNAPSHOT.jar /opt/demo-0.0.1/lib/demo-0.0.1-SNAPSHOT.jar
COPY common/config/demo.yml /opt/demo-0.0.1/etc/demo.yml
docker build . -f backend/Dockerfile …

When you run

docker build . --file backend/Dockerfile …
The path argument . becomes the context directory. (Docker actually sends itself a copy of this directory tree, which is where the /var/lib/docker/tmp/… path comes from.) The source arguments of COPY and ADD instructions are relative to the context directory, not relative to the Dockerfile.

If your source tree looks like

.
±- backend
| – Dockerfile
– target
– demo-0.0.1-SNAPSHOT.jar
that matches the Dockerfile you show. But if instead you have

.
±- backend
±- Dockerfile
– target
– demo-0.0.1-SNAPSHOT.jar
you’ll get the error you see.

If you don’t need to refer to anything outside of the context directory, you can just change what directory you’re passing to docker build

COPY target/demo-0.0.1-SNAPSHOT.jar /opt/demo-0.0.1/lib/demo-0.0.1-SNAPSHOT.jar
docker build backend …
Or, if you do have other content you need to copy in, you need to change the COPY paths to be relative to the topmost directory.

COPY backend/target/demo-0.0.1-SNAPSHOT.jar /opt/demo-0.0.1/lib/demo-0.0.1-SNAPSHOT.jar
COPY common/config/demo.yml /opt/demo-0.0.1/etc/demo.yml
docker build . -f backend/Dockerfile …