赞
踩
概述
在这篇文章中,我将分享如何从 Java 应用程序自动创建 docker 映像,并使用 Maven 将其推送到 docker 存储库。您还可以将各个步骤与 maven 目标相关联,以控制何时创建映像。还有其他方法,但我发现这种方法对开发人员来说非常容易。
先决条件
您应该安装以下软件:
码头工人。阅读 DZone 的 Refcard 了解 Docker 入门
JDK的。
Maven 的。
Docker文件。
访问 Docker 存储库(可选,仅当希望将映像推送到远程存储库时)。
第 1 步
定义一个 Dockerfile 并将其放在 pom.xml 所在的 Maven 项目根文件夹下。
DZone 之前介绍了如何使用 Jenkins 发布 Maven 工件。
步骤 2
在pom.xml中,在 下添加以下插件: …
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.6.0</version> <executions> <!-- Remove existing image from local repo --> <execution> <id>docker-clean</id> <phase>install</phase> <goals> <goal>exec</goal> </goals> <configuration> <executable>docker</executable> <workingDirectory>${project.basedir}</workingDirectory> <arguments> <argument>rmi</argument> <argument>${project.groupId}/${project.artifactId}:${project.version}</argument> </arguments> </configuration> </execution> <!-- Create new docker image using Dockerfile which must be present in current working directory. Tag the image using maven project version information. --> <execution> <id>docker-build</id> <phase>install</phase> <goals> <goal>exec</goal> </goals> <configuration> <executable>docker</executable> <workingDirectory>${project.basedir}</workingDirectory> <arguments> <argument>build</argument> <argument>-t</argument> <argument>${project.groupId}/${project.artifactId}:${project.version}</argument> <argument>.</argument> </arguments> </configuration> </execution> <!-- Login and Push the image to a docker repo. --> <execution> <id>docker-login</id> <phase>deploy</phase> <goals> <goal>exec</goal> </goals> <configuration> <executable>docker</executable> <workingDirectory>${project.basedir}</workingDirectory> <arguments> <argument>login</argument> <argument>-u</argument> <argument>${docker.user}</argument> <argument>-p</argument> <argument>${docker.password}</argument> <argument>${docker.url}</argument> </arguments> </configuration> </execution> <execution> <id>docker-push</id> <phase>deploy</phase> <goals> <goal>exec</goal> </goals> <configuration> <executable>docker</executable> <workingDirectory>${project.basedir}</workingDirectory> <arguments> <argument>push</argument> <argument>${project.groupId}/${project.artifactId}:${project.version}</argument> </arguments> </configuration> </execution> </executions> </plugin>
相关教程:如何部署 mule 应用程序
步骤 3
要在本地编译和构建 Docker 映像,请执行以下操作:
mvn clean install
要编译、生成映像并将其推送到远程存储库,请执行以下操作:
mvn clean deploy -Ddocker.user= -Ddocker.password=
-Ddocker.url=
就是这样! 如果您有任何问题或批评,请在下面发表评论!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。