Error while copying maven generated jar file in Docker file build stage

huangapple go评论66阅读模式
英文:

Error while copying maven generated jar file in Docker file build stage

问题

以下是翻译好的内容:

我有一个包含两个阶段的 Docker 文件。第一阶段使用 Maven 构建应用程序,第二阶段启动应用程序。文件内容如下:

FROM maven:3.8.3-openjdk-17 as maven_builder
WORKDIR /app
COPY up-user-service/pom.xml .
COPY up-user-service/src ./src
RUN mvn clean install
RUN ls -l /app/target

FROM openjdk:17-jdk-alpine as builder
COPY --from=maven_builder /app/target/*.jar up-user-service/target
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} application.jar
RUN java -Djarmode=layertools -jar application.jar extract
FROM openjdk:17-jdk-alpine
COPY --from=builder dependencies/ ./
RUN true
COPY --from=builder snapshot-dependencies/ ./
RUN true
COPY --from=builder spring-boot-loader/ ./
RUN true
COPY --from=builder application/ ./
ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]

然而,当我使用 Docker 文件构建时,出现错误 ERROR: failed to solve: lstat /var/lib/docker/tmp/buildkit-mount1400144723/target: no such file or directory。而且,我也无法看到 ls 阶段的输出,因为它从屏幕上很快消失。

有人能否建议可能的问题是什么?

英文:

I have a docker file having 2 stages. The first stage builds the application using maven and the second one start the application. The file contents are as follows:-

FROM maven:3.8.3-openjdk-17 as maven_builder
WORKDIR /app
COPY up-user-service/pom.xml .
COPY up-user-service/src ./src
RUN mvn clean install
RUN ls -l /app/target


FROM openjdk:17-jdk-alpine as builder
COPY --from=maven_builder /app/target/*.jar up-user-service/target
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} application.jar
RUN java -Djarmode=layertools -jar application.jar extract
FROM openjdk:17-jdk-alpine
COPY --from=builder dependencies/ ./
RUN true
COPY --from=builder snapshot-dependencies/ ./
RUN true
COPY --from=builder spring-boot-loader/ ./
RUN true
COPY --from=builder application/ ./
ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]

However, when I build this using docker file, I am getting the error ERROR: failed to solve: lstat /var/lib/docker/tmp/buildkit-mount1400144723/target: no such file or directory. I am also unable to see the output of ls stage also as it disappears very fast from screen.

Can someone please suggest what might be the issue here?

答案1

得分: 1

删除这两行

# 删除我!
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} application.jar

这些代码从主机系统复制一个*.jar文件,该文件位于与构建上下文相对应的target目录中的任何单个*.jar文件中;如果你在主机上没有单独构建应用程序,这将失败。

紧随其后是正确的一行

# 保留我!
COPY --from=maven_builder /app/target/*.jar up-user-service/target

它将由maven_builder阶段构建的jar文件复制过来。有了这一行,实际上已将jar文件包含在镜像中,第二组代码是多余的。

这里的另一个棘手的地方是,你需要在运行Spring解包器时将jar文件命名为application.jar,但构建输出可能会被命名为类似myapp-0.0.1.jar的东西。我可能会在构建阶段将其重命名:

FROM maven:3.8.3-openjdk-17 as maven_builder
...
RUN mvn clean install
RUN mv target/*.jar target/application.jar  # <-- 添加

FROM openjdk:17-jdk-alpine as builder
WORKDIR /app
COPY --from=maven_builder /app/target/application.jar ./
RUN java -Djarmode=layertools -jar application.jar extract

FROM openjdk:17-jdk-alpine
WORKDIR /app
COPY --from=builder /app/dependencies/ ./
...
英文:

Delete these two lines

# delete me!
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} application.jar

Those copy a jar file from your host system, any single *.jar file from the target directory relative to the build context; if you haven't separately built the application on your host this will fail.

Immediately before this you have the correct line

# keep me!
COPY --from=maven_builder /app/target/*.jar up-user-service/target

which copies the jar file built by the maven_builder stage. With that line the jar file is in fact in your image, the second set of lines is redundant.

The one other tricky thing here is that you require the jar file to be named application.jar when you run the Spring unpacker, but the build output will probably be named something like myapp-0.0.1.jar. I might rename this in the build stage:

FROM maven:3.8.3-openjdk-17 as maven_builder
...
RUN mvn clean install
RUN mv target/*.jar target/application.jar  # &lt;-- add

FROM openjdk:17-jdk-alpine as builder
WORKDIR /app
COPY --from=maven_builder /app/target/application.jar ./
RUN java -Djarmode=layertools -jar application.jar extract

FROM openjdk:17-jdk-alpine
WORKDIR /app
COPY --from=builder /app/dependencies/ ./
...

huangapple
  • 本文由 发表于 2023年5月22日 17:54:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76304976.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定