当前位置:   article > 正文

从 Maven 构建 Docker 镜像_maven docker

maven docker

概述
在这篇文章中,我将分享如何从 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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79

相关教程:如何部署 mule 应用程序

步骤 3
要在本地编译和构建 Docker 映像,请执行以下操作:
mvn clean install

要编译、生成映像并将其推送到远程存储库,请执行以下操作:
mvn clean deploy -Ddocker.user= -Ddocker.password=
-Ddocker.url=

就是这样! 如果您有任何问题或批评,请在下面发表评论!

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/866639
推荐阅读
相关标签
  

闽ICP备14008679号