[英]Adding Maven build steps into docker file for Java Jar?
I have a Java
project that builds a jar
file that is then ran on a Jboss EAP
server.我有一个
Java
项目,它构建一个jar
文件,然后在Jboss EAP
服务器上运行。 The jar
file is built using the maven command: jar
文件是使用 maven 命令构建的:
mvn clean install
Currently I am building the jar
manually using maven
then adding it to my dockerfile
as follows:目前,我正在使用
jar
手动构建maven
,然后将其添加到我的dockerfile
,如下所示:
# rest of dockerfile...
EXPOSE 8080 9990
ADD app.jar "/opt/jboss/jboss-eap-6.2/standalone/deployments/"
RUN ["chmod", "+rx", "/opt/jboss/jboss-eap-6.2/"]
ENTRYPOINT ["sh","/opt/jboss/jboss-eap-6.2/bin/standalone.sh"]
How can I add the maven
command into my dockerfile
so that a developer does not need to install maven and build it themselves first?如何将
maven
命令添加到我的dockerfile
中,以便开发人员无需先安装 maven 并自行构建?
ie the steps will just be:即步骤将只是:
1. checkout the code from git
2. run dockerfile
I think the best approach is to use a multi stage build similar to what is suggested here?我认为最好的方法是使用类似于此处建议的多阶段构建? However I am not sure how to implement this.
但是我不确定如何实现它。
The Dockerfile examples in Mark O'Connor's answer to the question you've linked to should be very close to what you need. Mark O'Connor 对您链接到的问题的回答中的 Dockerfile 示例应该非常接近您的需要。 Those are multistage builds because they have multiple
FROM
s in them.这些是多阶段构建,因为它们中有多个
FROM
。 The first stage uses a base image that already contains maven, so no need to install it: FROM maven:3.6.0-jdk-11-slim AS build
.第一阶段使用的基础镜像已经包含 maven,因此无需安装:
FROM maven:3.6.0-jdk-11-slim AS build
。
Your current Dockerfile content then becomes the second stage, but you will need to replace your ADD app.jar...
with a COPY
which picks the file out of the build
stage.您当前的 Dockerfile 内容将成为第二阶段,但您需要将
ADD app.jar...
替换为从build
阶段挑选文件的COPY
。 Something like就像是
COPY --from=build /path/to/mvn/output/app.jar \
/opt/jboss/jboss-eap-6.2/standalone/deployments/
The --from=build
refers to the AS build
from the earlier stage. --from=build
是指从早期阶段构建的AS build
。 No other files from the build stage will be copied into the later stage(s), so the final image will consist only of layers from the last FROM
onwards.构建阶段的其他文件不会复制到后续阶段,因此最终图像将仅包含最后一个
FROM
之后的层。
Editing to add: To address the question of how this prevents another user from needing to do any build and install manually: A new user can do this, instead.编辑添加:为了解决这如何防止其他用户需要手动进行任何构建和安装的问题:新用户可以这样做。 Assuming that Dockerfile lives in the root of your git project.
假设 Dockerfile 位于您的 git 项目的根目录中。
git clone <url> your-app
docker build -t your-img your-app # builds all stages
docker run --rm -it your-img # runs whatever the final stage produced
You can say to do all that in a Dockerfile. You can provide instructions to install maven inside Dockerfile first and then run whatever command you need from the installed maven.您可以说在 Dockerfile 中执行所有这些操作。您可以先提供在 Dockerfile 中安装 maven 的说明,然后从已安装的 maven 运行您需要的任何命令。
For instance,例如,
RUN cd /usr/local && wget -O- http://mirror.ox.ac.uk/sites/rsync.apache.org/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz | tar -xzf -
to install maven 3.6.3 and then later run a command that does what you need whether it be maven clean or whatever.安装 maven 3.6.3,然后运行一个命令来执行您需要的操作,无论它是 maven clean 还是其他什么。
I just provided the above command because I had it lying around, but if the version you want is available in yum, the other way would just be to use whatever package manager is installed.我只是提供了上面的命令,因为我有它,但是如果你想要的版本在 yum 中可用,另一种方法就是使用安装的任何 package 管理器。 Like
RUN yum --assumeyes install ________
.就像
RUN yum --assumeyes install ________
一样。
You should be able to do something similar with Git. I would read about best practices surrounding that.您应该能够使用 Git 执行类似的操作。我会阅读相关的最佳实践。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.