如何dockerize Maven 项目?以及有多少方法可以实现? [英] How to dockerize maven project? and how many ways to accomplish it?
问题描述
我是 Docker 的新手,尽管我阅读了很多文档并尝试了很多方法,但我不知道如何使用 maven 运行 java 项目.
- 我应该使用
Dockerfile
构建镜像吗? - 用
Dockerfile
在宿主机上运行maven项目的命令是什么?
工作示例.
这不是spring boot教程.这是关于如何在 Docker 容器中运行 Maven 构建的问题的更新答案.
问题最初发布于 4 年前.
1.生成应用程序
使用 spring 初始化器生成演示应用
在本地解压压缩包
2.创建一个 Dockerfile
<代码>## 构建阶段#FROM maven:3.6.0-jdk-11-slim AS build复制 src/home/app/src复制 pom.xml/home/appRUN mvn -f/home/app/pom.xml 清理包## 打包阶段#来自 openjdk:11-jre-slim复制 --from=build/home/app/target/demo-0.0.1-SNAPSHOT.jar/usr/local/lib/demo.jar曝光 8080入口点 ["java","-jar","/usr/local/lib/demo.jar"]
注意
- 此示例使用多阶段构建.第一阶段用于构建代码.第二个阶段只包含构建的 jar 和一个运行它的 JRE(注意 jar 是如何在阶段之间复制的).
3.构建镜像
docker build -t demo .
4.运行镜像
$ docker run --rm -it demo:latest.____ _ __ _/\/___'_ __ _ _(_)_ __ __ _ ( ( )\___ | '_ | '_| | '_ /_` | \/___)||_)|||||||(_| | ) ) ) )' |____|.__|_||_|_||_\__, |////==========|_|==============|___/=/_/_/_/:: Spring Boot :: (v2.1.3.RELEASE)2019-02-22 17:18:57.835 INFO 1 --- [ main] com.example.demo.DemoApplication :在 f4e67677c9a9 上启动 DemoApplication v0.0.1-SNAPSHOT,PID 为 1(/usr/local/bin/demo.jar 已启动根在/)2019-02-22 17:18:57.837 INFO 1 --- [ main] com.example.demo.DemoApplication :没有设置活动配置文件,回退到默认配置文件:默认2019-02-22 17:18:58.294 INFO 1 --- [main] com.example.demo.DemoApplication:在 0.711 秒内启动 DemoApplication(JVM 运行 1.035)
杂项
阅读 Docker 中心文档,了解如何优化 Maven 构建以使用本地存储库来缓存 jar.
更新 (2019-02-07)
这个问题已经问了 4 年了,在那个时候可以说使用 Docker 构建应用程序已经发生了重大变化.
选项 1:多阶段构建
这种新样式使您能够创建更轻量级的图像,而无需封装您的构建工具和源代码.
此处的示例再次使用 official maven 基础映像来运行第一阶段使用所需版本的 Maven 进行构建.文件的第二部分定义了如何将构建的 jar 组装到最终的输出图像中.
FROM maven:3.5-jdk-8 AS build复制 src/usr/src/app/src复制 pom.xml/usr/src/appRUN mvn -f/usr/src/app/pom.xml 清理包从 gcr.io/distroless/java复制 --from=build/usr/src/app/target/helloworld-1.0.0-SNAPSHOT.jar/usr/app/helloworld-1.0.0-SNAPSHOT.jar曝光 8080入口点 ["java","-jar","/usr/app/helloworld-1.0.0-SNAPSHOT.jar"]
注意:
- 我正在使用 Google 的 distroless 基础映像,它致力于为java应用程序.
选项 2:吊臂
我没有使用过这种方法,但似乎值得研究,因为它使您能够构建图像而无需创建诸如 Dockerfiles 之类的令人讨厌的东西:-)
https://github.com/GoogleContainerTools/jib
该项目有一个Maven 插件,它集成了打包将您的代码直接导入您的 Maven 工作流程.
<小时>原始答案(为了完整性而包含在内,但很久以前写的)
尝试使用新的官方镜像,Maven 有一个
https://registry.hub.docker.com/_/maven/
该映像可用于在构建时运行 Maven 以创建已编译的应用程序,或者如下例所示,在容器内运行 Maven 构建.
示例 1 - Maven 在容器中运行
以下命令在容器内运行您的 Maven 构建:
docker run -it --rm -v "$(pwd)":/opt/maven -w/opt/maven maven:3.2-jdk-7 mvn 全新安装
注意事项:
- 这种方法的巧妙之处在于,所有软件都在容器内安装和运行.只需要宿主机上的 docker.
- 请参阅此版本的Dockerfile
示例 2 - 使用 Nexus 缓存文件
运行 Nexus 容器
docker run -d -p 8081:8081 --name nexus sonatype/nexus
创建一个settings.xml"文件:
<镜子><镜子><id>nexus</id><mirrorOf>*</mirrorOf><url>http://nexus:8081/content/groups/public/</url></镜像></镜子></设置>
现在运行 Maven 链接到 nexus 容器,以便缓存依赖项
docker run -it --rm -v "$(pwd)":/opt/maven -w/opt/maven --link 连结:连结maven:3.2-jdk-7 mvn -s settings.xml 全新安装
注意事项:
- 在后台运行 Nexus 的一个优势是,可以通过管理 URL 透明地管理其他 3rd 方存储库,对在本地容器中运行的 Maven 构建而言.
I am new to Docker, and don't know how to run a java project with maven even though I have read many documents and tried many methods.
- Should I build the image using
Dockerfile
? - What is the commands like when it is to run the maven project in the host with
Dockerfile
?
Working example.
This is not a spring boot tutorial. It's the updated answer to a question on how to run a Maven build within a Docker container.
Question originally posted 4 years ago.
1. Generate an application
Use the spring initializer to generate a demo app
Extract the zip archive locally
2. Create a Dockerfile
#
# Build stage
#
FROM maven:3.6.0-jdk-11-slim AS build
COPY src /home/app/src
COPY pom.xml /home/app
RUN mvn -f /home/app/pom.xml clean package
#
# Package stage
#
FROM openjdk:11-jre-slim
COPY --from=build /home/app/target/demo-0.0.1-SNAPSHOT.jar /usr/local/lib/demo.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","/usr/local/lib/demo.jar"]
Note
- This example uses a multi-stage build. The first stage is used to build the code. The second stage only contains the built jar and a JRE to run it (note how jar is copied between stages).
3. Build the image
docker build -t demo .
4. Run the image
$ docker run --rm -it demo:latest
. ____ _ __ _ _
/\ / ___'_ __ _ _(_)_ __ __ _
( ( )\___ | '_ | '_| | '_ / _` |
\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.3.RELEASE)
2019-02-22 17:18:57.835 INFO 1 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication v0.0.1-SNAPSHOT on f4e67677c9a9 with PID 1 (/usr/local/bin/demo.jar started by root in /)
2019-02-22 17:18:57.837 INFO 1 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2019-02-22 17:18:58.294 INFO 1 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 0.711 seconds (JVM running for 1.035)
Misc
Read the Docker hub documentation on how the Maven build can be optimized to use a local repository to cache jars.
Update (2019-02-07)
This question is now 4 years old and in that time it's fair to say building application using Docker has undergone significant change.
Option 1: Multi-stage build
This new style enables you to create more light-weight images that don't encapsulate your build tools and source code.
The example here again uses the official maven base image to run first stage of the build using a desired version of Maven. The second part of the file defines how the built jar is assembled into the final output image.
FROM maven:3.5-jdk-8 AS build
COPY src /usr/src/app/src
COPY pom.xml /usr/src/app
RUN mvn -f /usr/src/app/pom.xml clean package
FROM gcr.io/distroless/java
COPY --from=build /usr/src/app/target/helloworld-1.0.0-SNAPSHOT.jar /usr/app/helloworld-1.0.0-SNAPSHOT.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","/usr/app/helloworld-1.0.0-SNAPSHOT.jar"]
Note:
- I'm using Google's distroless base image, which strives to provide just enough run-time for a java app.
Option 2: Jib
I haven't used this approach but seems worthy of investigation as it enables you to build images without having to create nasty things like Dockerfiles :-)
https://github.com/GoogleContainerTools/jib
The project has a Maven plugin which integrates the packaging of your code directly into your Maven workflow.
Original answer (Included for completeness, but written ages ago)
Try using the new official images, there's one for Maven
https://registry.hub.docker.com/_/maven/
The image can be used to run Maven at build time to create a compiled application or, as in the following examples, to run a Maven build within a container.
Example 1 - Maven running within a container
The following command runs your Maven build inside a container:
docker run -it --rm
-v "$(pwd)":/opt/maven
-w /opt/maven
maven:3.2-jdk-7
mvn clean install
Notes:
- The neat thing about this approach is that all software is installed and running within the container. Only need docker on the host machine.
- See Dockerfile for this version
Example 2 - Use Nexus to cache files
Run the Nexus container
docker run -d -p 8081:8081 --name nexus sonatype/nexus
Create a "settings.xml" file:
<settings>
<mirrors>
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://nexus:8081/content/groups/public/</url>
</mirror>
</mirrors>
</settings>
Now run Maven linking to the nexus container, so that dependencies will be cached
docker run -it --rm
-v "$(pwd)":/opt/maven
-w /opt/maven
--link nexus:nexus
maven:3.2-jdk-7
mvn -s settings.xml clean install
Notes:
- An advantage of running Nexus in the background is that other 3rd party repositories can be managed via the admin URL transparently to the Maven builds running in local containers.
这篇关于如何dockerize Maven 项目?以及有多少方法可以实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!