问题描述
我是Docker的新人,不知道如何使用maven运行java项目,尽管我已经阅读了许多文档并尝试了许多方法。- 我是否应该使用
Dockerfile
? - 在主机中使用
Dockerfile
?运行maven项目的命令是什么?
尝试使用新的官方图片,Maven有一个
该图像可用于在构建时运行Maven以创建编译的应用程序,或者在以下示例中,在容器内运行Maven构建。
示例1 - 在容器内运行的Maven
以下命令在容器内运行Maven构建:
$ pre> $ code> docker run -it --rm \
-v$(pwd):/ opt / maven \
-w / opt / maven \
maven:3.2-jdk-7 \
mvn clean install
注意:
- 关于这种方法的整洁事情是所有软件都在容器内安装并运行。主机上只需要docker。
- 此版本的Docker文件,
示例2 - 使用Nexus缓存文件
运行Nexus容器
docker run -d -p 8081:8081 --name nexus sonatype / nexus
创建一个settings.xml文件:
< settings>
< mirrors>
< mirror>
< id> nexus< / id>
< mirrorOf> *< / mirrorOf>
< url> http:// nexus:8081 / content / groups / public /< / url>
< / mirror>
< / mirrors>
< / settings>
现在运行Maven链接到nexus容器,以便依赖关系被缓存
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
注意:
- 运行Nexus的背景是其他第三方存储库可以通过管理URL透明地管理到在本地容器中运行的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
?
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.
- Dockerfile for this version, see
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 is the background is that other 3rd party repositories can be managed via the admin URL transparently to the Maven builds running in local containers.
这篇关于如何对maven项目进行dockerize?并有多少种方式来实现呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!