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

时间:2017-02-07 16:35:52

标签: docker fabric8 maven-docker-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是我的沙盒。

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

<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]
...

根据文档,与dockerFileDir一起使用的dockerFile应标记图片构建上下文。

2 个答案:

答案 0 :(得分:2)

我误解了fabric8io docker插件文档。您可以一起使用dockerFileDirdockerFile,但构建上下文不会设置为dockerFileDir值。要从target部署我的工件,我必须将Dockerfile放到项目的根目录中,使用项目根路径设置dockerFileDir值,从我的{删除dockerFile条目{1}}。我的sandbox以这种方式更新。下一步是使用pom conf中的assembly以更干净的方式实现相同目标。

答案 1 :(得分:0)

由于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>)中输入的名称。当然,您可以使用任何您想要的东西。