为什么GitLab CI / CD使用多阶段Dockerfile失败?

时间:2020-10-16 18:33:47

标签: docker gitlab dockerfile gitlab-ci

this Spring Blog post之后,我有一个多阶段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"]

它可以在docker build . --tag hello的本地环境中完美运行。

我想运行构建此Dockerfile的GitLab CI / CD管道,所以我的.gitlab-ci.yml是:

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/**/*

但是...它失败了,因为在Dockerfile的两个阶段之间找不到从构建器提取的文件夹。

我该如何解决这个问题并实施一个可正常运行的GitLab CI / CD,从而从该Dockerfile构建映像?


编辑:

如果我在第二个阶段的每个RUN ls application命令之后添加一个COPY命令,则:

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"]

GitLab CI / CD可以正常工作。这不是很奇怪吗?

0 个答案:

没有答案