Docker build multistage - failed to compute cache key

Hi Team - We are trying to build a docker image and this is a Multistage build.

docker build is erroring out with the below message

failed to compute cache key: failed to walk /opt/data/docker/data/tmp/buildkit-mount2977242485/usr/lib/.build-id/01/e11c545160c74b88b9ce9259e7467863eab9e7: lstat /opt/data/docker/data/tmp/buildkit-mount2977242485/usr/lib/.build-id/01/e11c545160c74b88b9ce9259e7467863eab9e7: no such file or directory

We are using BUILDKIT feature.

Any help on this is really appreciated.

Thanks
Madhu

Without seeing your Dockerfile the only thing that remains is: your COPY instruction uses a wrong path, like the error message indicates.

Sorry. Here is my Dockerfile

FROM xxxxxx/docker/openshift-poc/jre:ubi8-minimal-jre17 AS build-stage
ARG JAR_FILE=/target/*.jar
COPY ${JAR_FILE} application.jar

RUN java -Djarmode=layertools -jar application.jar extract

FROM xxxxx/docker/openshift-poc/jre:ubi8-minimal-jre17
COPY --from=build-stage dependencies/ /tmp
COPY --from=build-stage snapshot-dependencies/ /tmp
COPY --from=build-stage spring-boot-loader/ /tmp
COPY --from=build-stage application/ /tmp
ENTRYPOINT [“java”, “org.springframework.boot.loader.JarLauncher”]

Thanks

Please use preformated text to format Dockerfile content.

I would suggest to use a second RUN instruction on your build-stage with RUN echo ${PWD} && ls -lR

Most likely the problem is that the COPY instructions do not use absolute paths for the source folders.

I can’t find it in the documentation, but the example in the docs is using absolute path and I have never tried to copy from another stage without absolute path as a source. Never even thought of doing it so I agree with @meyay, that is a very likely cause of the error message.

I forgot to mention why :slight_smile: The motivation is to get debug information, so you can see the current location as absolute path and in which subfolder the folders you try to copy are.

Of course, once you identified how the absolute paths need to look like, you can remove the RUN instruction again.

Sure. thanks for that info.

let me try that and update the results here.

The interesting thing is, If I am changing my BASE image, things are working fine.

Regards,
Madhu

Here is the output I see after adding the second RUN instruction.

=> [build-stage 4/4] RUN echo ${PWD} && java -Djarmode=layertools -jar application.jar extract && echo ${PWD} && ls -lR 1.7s
=> => # -rw-r–r–. 1 root root 293 Feb 1 1980 JarMode.class
=> => # -rw-r–r–. 1 root root 2201 Feb 1 1980 JarModeLauncher.class
=> => # -rw-r–r–. 1 root root 1292 Feb 1 1980 TestJarMode.class
=> => # ./spring-boot-loader/org/springframework/boot/loader/util:
=> => # total 8
=> => # -rw-r–r–. 1 root root 5174 Feb 1 1980 SystemPropertyUtils.class

###############################

I removed the second stage in my Dockerfile and built the image…I see the folders that I am trying to copy in the second stage are available on the container filesystem under /

drwxr-xr-x. 2 root root 6 Jan 26 11:33 snapshot-dependencies
drwxr-xr-x. 3 root root 16 Jan 26 11:33 spring-boot-loader
drwxr-xr-x. 4 root root 36 Jan 26 11:33 application
drwxr-xr-x. 3 root root 21 Jan 26 11:33 dependencies
[root@5917b498bdfe /]# pwd
/

#####################################################

Thanks

#7 [builder 3/3] RUN java -Djarmode=layertools -jar application.jar extract && echo ${PWD} && ls -l
#7 sha256:219e16016c8c23ebfa340a15a5377d845e0c04a014b63000e0dccace803de9a6
#7 1.635 /
#7 1.640 total 53176
#7 1.640 drwxr-xr-x. 4 root root 36 Jan 26 15:38 application
#7 1.640 -rw-r–r–. 1 root root 54434855 Jan 26 11:06 application.jar
#7 1.640 lrwxrwxrwx. 1 root root 7 Jun 21 2021 bin → usr/bin
#7 1.640 dr-xr-xr-x. 2 root root 6 Jun 21 2021 boot
#7 1.640 drwxr-xr-x. 3 root root 21 Jan 26 15:38 dependencies
#7 1.640 drwxr-xr-x. 5 root root 340 Jan 26 15:38 dev
#7 1.640 drwxr-xr-x. 1 root root 4096 Dec 15 13:37 etc
#7 1.640 drwxr-xr-x. 2 root root 6 Jun 21 2021 home
#7 1.640 lrwxrwxrwx. 1 root root 7 Jun 21 2021 lib → usr/lib
#7 1.640 lrwxrwxrwx. 1 root root 9 Jun 21 2021 lib64 → usr/lib64
#7 1.640 drwx------. 2 root root 6 Sep 6 12:17 lost+found
#7 1.640 drwxr-xr-x. 2 root root 6 Jun 21 2021 media
#7 1.640 drwxr-xr-x. 2 root root 6 Jun 21 2021 mnt
#7 1.640 drwxr-xr-x. 1 root root 17 Dec 15 13:37 opt
#7 1.640 dr-xr-xr-x. 236 root root 0 Jan 26 15:38 proc
#7 1.640 dr-xr-x—. 3 root root 4096 Sep 6 12:24 root
#7 1.640 drwxr-xr-x. 1 root root 4096 Dec 15 13:37 run
#7 1.640 lrwxrwxrwx. 1 root root 8 Jun 21 2021 sbin → usr/sbin
#7 1.640 drwxr-xr-x. 2 root root 6 Jan 26 15:38 snapshot-dependencies
#7 1.640 drwxr-xr-x. 3 root root 16 Jan 26 15:38 spring-boot-loader
#7 1.640 drwxr-xr-x. 2 root root 6 Jun 21 2021 srv
#7 1.640 dr-xr-xr-x. 13 root root 0 Oct 31 20:39 sys
#7 1.640 drwxrwxrwt. 1 root root 28 Jan 26 15:38 tmp
#7 1.640 drwxr-xr-x. 1 root root 75 Sep 6 12:17 usr
#7 1.640 drwxr-xr-x. 1 root root 38 Sep 6 12:17 var
#7 DONE 2.5s

#8 [stage-1 2/5] COPY --from=builder dependencies/ /
#8 sha256:efe19f075625e924493bbec32599cb5eecda4227631d78004b9352975f2aa728
#8 CACHED

#9 [stage-1 3/5] COPY --from=builder snapshot-dependencies/ /
#9 sha256:576ab9b6d9a000b79634f5e01b190f2dfe6ec9836fcc8c2be70dd69e34d1e571
#9 ERROR: failed to walk /opt/data/docker/data/tmp/buildkit-mount4132688350/usr/lib/.build-id/01/e11c545160c74b88b9ce9259e7467863eab9e7: lstat /opt/data/docker/data/tmp/buildkit-mount4132688350/usr/lib/.build-id/01/e11c545160c74b88b9ce9259e7467863eab9e7: no such file or directory

#8 [stage-1 2/5] COPY --from=builder dependencies/ /
#8 sha256:efe19f075625e924493bbec32599cb5eecda4227631d78004b9352975f2aa728
#8 ERROR: failed to walk /opt/data/docker/data/tmp/buildkit-mount4136272112/usr/lib/.build-id/01/e11c545160c74b88b9ce9259e7467863eab9e7: lstat /opt/data/docker/data/tmp/buildkit-mount4136272112/usr/lib/.build-id/01/e11c545160c74b88b9ce9259e7467863eab9e7: no such file or directory

Thanks

Please format the outputs according this post, so it gets easier to read.

It is like @rimelek and I already wrote: it’s the lack of absolute paths.

Try:

FROM xxxxxx/docker/openshift-poc/jre:ubi8-minimal-jre17 AS build-stage
ARG JAR_FILE=/target/*.jar
COPY ${JAR_FILE} application.jar

RUN java -Djarmode=layertools -jar application.jar extract

FROM xxxxx/docker/openshift-poc/jre:ubi8-minimal-jre17
COPY --from=build-stage /dependencies/ /tmp/
COPY --from=build-stage /snapshot-dependencies/ /tmp/
COPY --from=build-stage /spring-boot-loader/ /tmp/
COPY --from=build-stage /application/ /tmp/
ENTRYPOINT [“java”, “org.springframework.boot.loader.JarLauncher”]

Tried with above Dockefile and still same error.

Regards,
Madhu M

Also another thing that I noticed is, same Dockerfile is working fine when I try to run/build the image from different machine.

So I am not sure what’s going on on the machine where the build is failing.

Regards,
Madhu M

Are you sure your machine uses an official Docker Distribution (Docker Desktop/Docker-CE) from Docker’s official repositories?

Please do not ignore this.

Machine where this is failing:

Client: Docker Engine - Community
 Version:           20.10.21
 API version:       1.41
 Go version:        go1.18.7
 Git commit:        baeda1f
 Built:             Tue Oct 25 18:04:24 2022
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.21
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.18.7
  Git commit:       3056208
  Built:            Tue Oct 25 18:02:38 2022
  OS/Arch:          linux/amd64
  Experimental:     true
 containerd:
  Version:          1.6.9
  GitCommit:        1c90a442489720eec95342e1789ee8a5e1b9536f
 runc:
  Version:          1.1.4
  GitCommit:        v1.1.4-0-g5fd4c4d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

and Machine on which same Dockerfile is working

Client: Docker Engine - Community
 Version:           20.10.21
 API version:       1.41
 Go version:        go1.18.7
 Git commit:        baeda1f
 Built:             Tue Oct 25 18:02:19 2022
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.21
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.18.7
  Git commit:       3056208
  Built:            Tue Oct 25 18:00:24 2022
  OS/Arch:          linux/amd64
  Experimental:     true
 containerd:
  Version:          1.6.11
  GitCommit:        d986545181c905378b0f90faa9c5eae3cbfa3755
 runc:
  Version:          1.1.4
  GitCommit:        v1.1.4-0-g5fd4c4d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

I am experiencing a similar problem with my M2 Apple Silicon Chip, which may be connected to COPY instruction is not cached on linux/arm64 when cache is generated on linux/amd64 machine · Issue #1921 · docker/buildx · GitHub

@madhu3984 please, were you able to find a solution to the issue? I’m currently facing the same issue

I have a similar problem when doing a multi-stage build on Ubuntu on WSL2 on Windows 11 with Docker Desktop.

My Dockerfile

FROM node:latest as builder
WORKDIR /usr/src/app
COPY . .
RUN npm install
RUN npm run build
RUN cp -r /usr/src/app/.output/ /output/

FROM node:latest
WORKDIR /usr/src/app
COPY --from=builder /ouput/nitro.json .

I open Ubuntu, then docker build to stage ‘builder’ and start a container from that image manually in the Windows interface of Docker Desktop. I have confirmed that the build-image contains the file /output/nitro.json.

Then I do a docker build for the whole file. Still COPY --from=builder /ouput/nitro.json . gives the following error

ERROR: failed to solve: failed to compute cache key: failed to calculate checksum of ref c290b074-c322-479c-b461-44419293caef::te2yh1nks699h0puaas9890vq: failed to walk /var/lib/docker/tmp/buildkit-mount2099609999/ouput: lstat /var/lib/docker/tmp/buildkit-mount2099609999/ouput: no such file or directory

It seems like the COPY --from command tries to pull the files from a folder that does not exist in the Ubuntu-context.

vs

There is a “t” missing