英文:
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 ["java", "-jar", "/app.jar"]
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 <repo>/<first_docker_file>: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 <repo>/<first_docker_file>: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.
专注分享java语言的经验与见解,让所有开发者获益!
评论