Why the GitLab CI/CD fail with multistage Dockerfile?

Following this Spring Blog post I have a multistage Dockerfile:

FROM adoptopenjdk:11-jre-hotspot as builder
WORKDIR application
ARG JAR_FILE=./hello/build/libs/*.jar
COPY ${JAR_FILE} application.jar
RUN java -Djarmode=layertools -jar application.jar extract
ADD ./dependencies/ ./


FROM adoptopenjdk:11-jre-hotspot
WORKDIR application
COPY --from=builder application/dependencies/ ./
COPY --from=builder application/snapshot-dependencies/ ./
# COPY --from=builder application/resources/ ./
COPY --from=builder application/spring-boot-loader/ ./
COPY --from=builder application/application/ ./
ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]

It works perfectly in a local environment with: docker build . --tag demo.

I want run a GitLab CI/CD pipeline that build this Dockerfile, so my .gitlab-ci.yml is:

image: openjdk:11

variables:
  DOCKER_DRIVER: overlay
  GRADLE_OPTS: "-Dorg.gradle.daemon=false"

stages:
  - build
  - release

build hello:
  stage: build
  script:
    - cd ./hello
    - ./gradlew build -x test --stacktrace
  artifacts:
    paths:
      - ./hello/build/libs/*.jar
  only:
    changes:
      - hello/**/*

release hello:
  image: docker:latest
  services:
    - docker:dind
  stage: release
  script:
    - docker build -t registry.gitlab.com/myproject/hello --file ./Dockerfile .
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker push registry.gitlab.com/myproject/hello
  only:
    changes:
      - hello/**/*

but… it fail because between the two stages of the Dockerfile the extracted (from the builder) folders can’t be found.

How can I solve this and implement a working GitLab CI/CD that build the image from that Dockerfile?

If I add a RUN ls application command, after every COPY command, in the second phase:

FROM adoptopenjdk:11-jre-hotspot AS builder
WORKDIR application
ARG JAR_FILE=./hello/build/libs/*.jar
COPY ${JAR_FILE} application.jar
RUN java -Djarmode=layertools -jar application.jar extract


FROM adoptopenjdk:11-jre-hotspot
WORKDIR application
COPY --from=builder application/dependencies ./
RUN ls application
COPY --from=builder application/snapshot-dependencies ./
RUN ls application
# COPY --from=builder application/resources ./
COPY --from=builder application/spring-boot-loader ./
RUN ls application
COPY --from=builder application/application ./
RUN ls application
ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]

The GitLab CI/CD works correctly. Isn’t this weird?

This is happening to me as well:

ARG ARCH=
FROM node:16-alpine as builder
ENV NODE_ENV=development
WORKDIR ./home/node
COPY ["package.json", "yarn.lock", "./"]
RUN ls -lah ./
RUN yarn install
COPY . .
RUN ls -lah ./
RUN yarn build
FROM ${ARCH}node:16-alpine
WORKDIR ./home/node
COPY --from=builder /home/node/ ./
RUN ls -lah ./
CMD ["node", "./build/index.js"]

@kambei 's fix does not work for me, as my second stage targets a different cpu architecture than my CI host and cannot execute the ls that’s bundled with the container image (architecture mismatch)