通过 fabric8io docker-maven-plugin 将 .war 部署到 Docker 图像中

Deploy a .war into a Docker image by fabric8io docker-maven-plugin

我对 Docker 和 fabric8io docker-maven-plugin 很陌生。我很难将 .war 部署到由 jboss/wildfly 派生的图像中。我可以通过命令行成功构建 .war 附带的图像,但我不能通过已经提到的插件来做同样的事情。 据我所知,一旦你给出了正确的上下文并且 Dockerfile 它应该只是一个问题:

<build>
  <finalName>${project.build.finalName}.${project.packaging}</finalName>
  ...
  <assembly>
    <descriptorRef>artifact</descriptorRef>
  </assembly>
</build>

但是我遇到了这个错误:

[INFO] <<< docker-maven-plugin:0.19.0:build (default-cli) < package @ YoOffer <<<
[INFO] 
[INFO] --- docker-maven-plugin:0.19.0:build (default-cli) @ YoOffer ---
[WARNING] Cannot include project artifact: edu.pezzati.yo:YoOffer:war:0.0.1-SNAPSHOT; it doesn't have an associated file or directory.
[WARNING] The following patterns were never triggered in this artifact inclusion filter:
o  'edu.pezzati.yo:YoOffer'

[ERROR] DOCKER> Failed to create assembly for docker image  (with mode 'dir'): Error creating assembly archive docker: You must set at least one file.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

Here 是我的沙箱。

更新: 我清理了 pom 中的一些乱七八糟的东西。现在看起来像这样:

<images>
    <image>
        <alias>yowildfly</alias>
        <name>jboss/wildfly</name>
        <build>
        <!--
            <assembly>
                <descriptorRef>${project.basedir}/src/test/resources/DOCKER/assembly.xml</descriptorRef>
            </assembly>
        -->
            <args>
                <webapp>target/${project.build.finalName}.${project.packaging}</webapp>
            </args>
            <dockerFileDir>${project.basedir}</dockerFileDir>
            <dockerFile>src/test/resources/DOCKER/wildfly/Dockerfile</dockerFile>
            </build>
            <run>
                <ports>
                    <port>8080:8080</port>
                </ports>
            </run>
        </image>
...
</images>

现在我收到这个错误:

...    
[ERROR] DOCKER> Unable to build image [jboss/wildfly]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.769 s
[INFO] Finished at: 2017-02-12T01:19:57+01:00
[INFO] Final Memory: 36M/271M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal io.fabric8:docker-maven-plugin:0.19.0:build (default-cli) on project YoOffer: Unable to build image [jboss/wildfly]: lstat target/YoOffer.war: no such file or directory -> [Help 1]
[ERROR]
...

根据文档,dockerFileDirdockerFile 一起使用应该标记图像构建上下文。

我误解了 fabric8io docker 插件文档。您可以一起使用 dockerFileDirdockerFile 但构建上下文不会设置为 dockerFileDir 值。要从 target 部署我的工件,我必须将我的 Dockerfile 放到我的项目的根目录中,使用项目根路径设置 dockerFileDir 值,从我的 pom。我的sandbox就是这样更新的。下一步是通过在 build conf.

中使用 assembly 以更简洁的方式实现相同的目的

由于 Docker 的工作方式,您应该可以将 Dockerfile 放在任何您想要的地方,没有任何限制。因此,在 io.fabric8 maven 插件中有一个选项,允许您在 docker 图像构建期间复制所需的文件。这就是它对我有用的方式:

  1. 我决定将我的 Dockerfile 放入 src/main/docker
  2. 在 docker 镜像构建过程中我需要两个文件:
    • target/SomeName.war(这是打包maven阶段的产物)
    • src/main/resources/OtherFiles.xml(这只是我也需要的另一种资源)

pom.xml 文件中的 io.fabric8 插件配置:

<plugin>
  <groupId>io.fabric8</groupId>
  <artifactId>docker-maven-plugin</artifactId>
  <version>0.24.0</version>
  <configuration>
    <images>
      <image>
        <name>some/name</name>
        <build>
          <dockerFileDir>${project.basedir}/src/main/docker</dockerFileDir>
          <dockerFile>Dockerfile</dockerFile>
          <assembly>
            <mode>dir</mode>
            <name>maven/</name>
            <inline xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
              <id>middleware-rest</id>
              <files>
                <file>
                  <source>${project.build.directory}/SomeName.war</source>
                  <outputDirectory>./</outputDirectory>
                  <destName>SomeName.war</destName>
                </file>
                <file>
                  <source>${project.basedir}/src/test/resources/OtherFiles.xml</source>
                  <outputDirectory>./</outputDirectory>
                  <destName>OtherFiles.xml</destName>
                </file>
              </files>
            </inline>
          </assembly>
        </build>
        <run>
          <ports>
            <port>8080:8080</port>
          </ports>
          <network>
            <mode>host</mode>
          </network>
          <wait>
            <log>.*WildFly .* \(WildFly .*\) started in [0-9]*ms - Started [0-9]* of [0-9]* services</log>
            <time>360000</time>
          </wait>
        </run>
      </image>
    </images>
    <showLogs>true</showLogs>
  </configuration>
  <!-- Connect start/stop to pre- and post-integration-test phase, respectively if you want to start your docker containers during integration tests -->
  <executions>
    <execution>
      <id>start</id>
      <phase>pre-integration-test</phase>
      <goals>
        <!-- "build" should be used to create the images with the artifact -->
        <goal>build</goal>
        <goal>start</goal>
      </goals>
    </execution>
    <execution>
      <id>stop</id>
      <phase>post-integration-test</phase>
      <goals>
        <goal>stop</goal>
      </goals>
    </execution>
  </executions>
</plugin>

最后是我如何在 Dockerfile 中引用这些文件:

ADD maven/SomeName.war /opt/jboss/wildfly/standalone/deployments/

请注意,maven 是您在 pom.xml (<name>maven/</name>) 中输入的名称。当然你可以使用任何你想要的。