perotom
perotom

Reputation: 923

Maven run docker image with java ee application

I have a running java ee application which is using wildfly and mysql. Now i heard that docker is using everyone and it is very productive so i decided to dockerize my development environment. Sounds easier than it is.

What i have so far:

What i want:

I started out by building an docker image with the maven-docker-plugin:

<plugin>
   <groupId>com.spotify</groupId>
   <artifactId>docker-maven-plugin</artifactId>
   <version>0.4.13</version>
   <configuration>
      <imageName>netbeans/sampleapplication</imageName>
      <dockerDirectory>src/main/docker</dockerDirectory>
      <resources>
         <resource>
            <targetPath>/</targetPath>
            <directory>${project.build.directory}</directory>
            <include>${project.build.finalName}.war</include>
         </resource>
      </resources>
      <execution>
         <id>build-image</id>
         <phase>package</phase>
         <goals>
            <goal>build</goal>
         </goals>
      </execution>
   </configuration>
</plugin>

Dockerfile:

FROM jboss/wildfly
COPY *.war /opt/jboss/wildfly/standalone/deployments/app.war
EXPOSE 8080 9990

Maven command: clean package docker:build. I can reach the application server just with my docker-maschine url and not like previously with localhost. In the end i just want to use a single maven command for:

  1. Building the application
  2. Building the docker images (wildfly,mysql...)
  3. Run arquillian junit tests
  4. Deploy the application and expose it via localhost:8080
  5. Stop container if a new deploy is made

I am really struggling with that. Anbody an idea how to do this?

Upvotes: 1

Views: 1193

Answers (2)

gtonic
gtonic

Reputation: 2313

Alternatively to directly using a JBoss Wildfly container, you also might check out Wildfly Swarm. It's a separate distribution of Wildfly with even more goodies regarding docker.

Upvotes: 1

wemu
wemu

Reputation: 8160

there is no straight forward way of doing this - since some of the docker tasks cannot easily be mapped to a maven phase. So you need to choose what a preferred way of working for you is.

So some thoughts that hopefully will lead to a solution:

The spotify-docker-maven plugin has no mojo's (maven goals) to run an image. Its main tasks are about creating and publishing the docker images.

So to run an image you can simply write some bash scripts (since they will be simple, they will run on linux and even windows using the git bash command line). You could execute those scripts using the maven-exec-plugin.

To properly map that to the maven lifecycle is a bit more tricky.

The phase that matches this the best (my opinion only) is the integration-test phase. This phase has a pre-integration-test phase, the integration-test phase and a post-integration-test phase. The idea is to startup the containers in the pre- phase. Then run the tests in the integration test phase using the failsafe-plugin (not letting the build fail!) and cleaning up the containers in the post- phase. It will be a good idea to cleanup the containers of that project in the pre- phase as well - just in case some zombie containers stick around.

These steps could be put into a profile. Since the integration-test phase is needed for integration tests as well, one would end up executing "maven verify" with different profiles (mvn verify && mvn verify -P docker-tests && mvn -P docker-other-tests).

Another approach would be using the maven plugin created by fabric8. This plugin is a bit more complicated than the one created by spotify (again: my opinion only). But it comes along with more goals.

Using the provided <packaging>docker</packaging> of the plugin the docker run and stop goals are already mapped to the lifecycle.

Both plugins have in the end a similar complexity in the pom.xml - just its more reading with the fabric plugin. But there are some nice examples and a good user manual.

So those are the two options that came to my mind. Hope this will help :)

Upvotes: 3

Related Questions