如何使用Docker的onbuild copy将一个jar文件从一个Docker阶段复制到另一个阶段?

huangapple 未分类评论44阅读模式
英文:

How to copy a jar-file from one docker stage to another with docker onbuild copy?

问题

我是Docker的初学者,我正在尝试分两个阶段构建一个镜像。
第一个Docker文件已上传到Docker Hub,并具有以下结构:

    FROM openjdk:8-jdk-alpine 
    ONBUILD COPY app.jar /app.jar
    CMD ["java", "-jar", "/app.jar"]

第二个Docker文件:

    FROM gradle:4.7.0-jdk8-alpine AS build
    COPY --chown=gradle:gradle . /home/gradle/src
    WORKDIR /home/gradle/src
    RUN ./gradlew build

    FROM <repo>/<first_docker_file>:1.0
    COPY --from=build /home/gradle/src/build/libs/*.jar /app.jar

因此,我需要构建我的项目->生成jar文件->将其复制到第二个Docker文件的第二个阶段的根目录。
我需要将生成的jar文件从第一个阶段复制到第二个阶段的根目录,因为"ONBUILD COPY app.jar /app.jar"被触发,并且正在等待根目录中的app.jar文件。我在第二个文件中提供的代码不起作用。我也不能更改第一个Docker文件的代码。您有任何想法,如何使其工作以及在第二个Docker文件中应进行哪些更改?
英文:

I'm beginner with Docker, and I'm trying to build an image in two stages.
The first docker-file is uploaded to docker-hub and has the following structure:

FROM openjdk:8-jdk-alpine 
ONBUILD COPY app.jar /app.jar
CMD [&quot;java&quot;, &quot;-jar&quot;, &quot;/app.jar&quot;]

The second dockerfile:

FROM gradle:4.7.0-jdk8-alpine AS build
COPY --chown=gradle:gradle . /home/gradle/src
WORKDIR /home/gradle/src
RUN ./gradlew build

FROM &lt;repo&gt;/&lt;first_docker_file&gt;:1.0
COPY --from=build /home/gradle/src/build/libs/*.jar /app.jar

So I need to build my project -> generate jar file -> copy it to the root directory of the second docker stage of the second docker file.
I need to copy generated jar-file from the first stage to the root directory of the second stage, cause "ONBUILD COPY app.jar /app.jar" is triggered and is waiting for app.jar file in the root directory. The code I have provided in the second file does not work. I also can't change the code of the first docker-file. Do you have any ideas, how can I make it work and what should I change in the second docker file ?

答案1

得分: 0

根据ONBUILD参考文档,我理解在继承自FROM命令后,ONBUILD命令会直接插入。

据我理解,这意味着当发出FROM命令时,您的app.jar需要存在,即您的有效Dockerfile看起来会像这样:

FROM <repo>/<first_docker_file>:1.0
COPY app.jar /app.jar
COPY --from=build /home/gradle/src/build/libs/*.jar /app.jar
[...]

显然这是行不通的。

似乎您的第一个Dockerfile并不适用于分阶段构建,您需要确保在发出第一个Dockerfile上的构建时,app.jar位于工作目录中。也就是说,通过在容器内进行编译并将app.jar复制到第一个Dockerfile位置,然后再进行构建。

英文:

From the ONBUILD reference i understand that the ONBUILD commands are inserted directly after the FROM command when inheriting from it.

As i understand it this means your app.jar needs to exist when the FROM command is issued, i.e. your effective Dockerfile would look like

FROM &lt;repo&gt;/&lt;first_docker_file&gt;:1.0
COPY app.jar /app.jar
COPY --from=build /home/gradle/src/build/libs/*.jar /app.jar
[...]

which obviously wouldn't work.

It seems like your first dockerfile is not meant to be used in a staged build, you need to get the app.jar in the working directory when issuing the build on the first dockerfile, i.e. by compiling inside a container and copying out your app.jar to your first dockerfile location and then build it.

huangapple
  • 本文由 发表于 2020年7月27日 06:19:59
  • 转载请务必保留本文链接:https://java.coder-hub.com/63106338.html
匿名

发表评论

匿名网友

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

确定