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