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?