Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am very new to Docker and the fabric8io docker-maven-plugin. I am having some hard times to deploy a .war into my image derived by the jboss/wildfly one. I can succesful build my image shipped with .war by command line but I can't do the same by the already mentioned plugin. As far as I read, once you give right context and Dockerfile it should be only a matter of:

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

but I am gettin this error:

[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 is my sandbox.

UPDATE: I clean up some mess in my pom. Now it looks like this:

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

Now I am getting this error:

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

Accordingly to docs, dockerFileDir used together with dockerFile should mark image build context.

like image 767
Francesco Avatar asked Feb 07 '17 16:02

Francesco


People also ask

What is fabric8 Maven plugin?

The fabric8-maven-plugin (f8-m-p) brings your Java applications on to Kubernetes and OpenShift. It provides a tight integration into Maven and benefits from the build configuration already provided. This plugin focus on two tasks: Building Docker images and creating Kubernetes and OpenShift resource descriptors.

What is Docker Maven plugin?

This is a Maven plugin for building Docker images and managing containers for integration tests. It works with Maven 3.0. 5 and Docker 1.6. 0 or later.

What is the difference between Maven and Docker?

Docker belongs to "Virtual Machine Platforms & Containers" category of the tech stack, while Apache Maven can be primarily classified under "Java Build Tools". Some of the features offered by Docker are: Integrated developer tools. open, portable images.


2 Answers

You should be able to put your Dockerfile wherever you want to have it without any limitations due to how Docker works. Therefore, there is an option in the io.fabric8 maven plugin which allows you to copy the files you need during docker image building. This is how it worked for me:

  1. I decided to put my Dockerfile into src/main/docker
  2. I need two files during docker image build:
    • target/SomeName.war (this is a product of the package maven phase)
    • src/main/resources/OtherFiles.xml (this is simply another resource I also need)

io.fabric8 plugin configuration in pom.xml file:

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

And finally how I reference this files in the Dockerfile:

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

Note that maven is the name you put in the pom.xml (<name>maven/</name>). Of course you can use whatever you want instead.

like image 114
Manuel Schmidt Avatar answered Oct 15 '22 22:10

Manuel Schmidt


I misunderstood fabric8io docker plugin docs. You can use together dockerFileDir and dockerFile but building context won't be set to dockerFileDir value. To deploy my artifact from target I have to put my Dockerfile to the root of my project, set dockerFileDir value with project root path, remove the dockerFile entry from my pom. My sandbox is updated this way. Next step is to achieve the same in a cleaner way by using assembly in build conf.

like image 26
Francesco Avatar answered Oct 15 '22 22:10

Francesco