Java EE, Docker and Maven (Tech Tip #89)

Java EE applications are typically built and packaged using Maven. For example, github.com/javaee-samples/javaee7-docker-maven is a trivial Java EE 7 application and shows the Java EE 7 dependency:

And the two Maven plugins that compiles the source and builds the WAR file:

This application can then be deployed to a Java EE 7 container, such as WildFly, using the wildfly-maven-plugin:

Tests can be invoked using Arquillian, again using Maven. So if you were to package this application as a Docker image and run it inside a Docker container, there should be a mechanism to seamlessly integrate in the Maven workflow.

Docker Maven Plugin

Meet docker-maven-plugin!

This plugin allows you to manage Docker images and containers from your pom.xml. It comes with predefined goals:

Goal Description
docker:start Create and start containers
docker:stop Stop and destroy containers
docker:build Build images
docker:push Push images to a registry
docker:remove Remove images from local docker host
docker:logs Show container logs

Introduction provides a high level introduction to the plugin including building images, running containers and configuration.

Run Java EE 7 Application as Docker Container using Maven

TLDR;

  1. Create and Configure a Docker Machine as explained in Docker Machine to Setup Docker Host
  2. Clone the workspace as: git clone https://github.com/javaee-samples/javaee7-docker-maven.git
  3. Build the Docker image as: mvn package -Pdocker
  4. Run the Docker container as: mvn install -Pdocker
  5. Find out IP address of the Docker Machine as: docker-machine ip mydocker
  6. Access your application

Docker Maven Plugin Configuration

Lets look little deeper in our sample application.

pom.xml is updated to include docker-maven-plugin as:

Each image configuration has three parts:

  • Image name and alias
  • <build> that defines how the image is created. Base image, build artifacts and their dependencies, ports to be exposed, etc to be included in the image are specified here.Assembly descriptor format is used to specify the artifacts to be included and is defined in src/main/docker directory. assembly.xml in our case looks like:
  • <run> that defines how the container is run. Ports that need to be exposed are specified here.

In addition, package phase is tied to docker:build goal and install phase is tied to docker:start goal.

There are four docker-maven-plugin and you can read more details in the shootout on what serves your purpose the best.

How are you creating your Docker images from existing applications?

Enjoy!

 

Be Sociable, Share!
  • Tweet

8 thoughts on “Java EE, Docker and Maven (Tech Tip #89)

  1. HI Arun! Excellent work, as always. I only wanted to remark that the version of docker maven plugin that worked for me was 0.11.4. The 0.11.5-M1 version raised an error having the image name as null.
    Thanks a million!

  2. Hi Arun. Just wondering how your development setup looks like with Java & Docker. We have a setup with a Nginx reverse proxy in front of the various pieces of our app (e.g. OAuth auth server & resource server). We are having difficulties with our traditional approach though of running the Java web apps locally in our IDE. e.g. the traffic goes through Nginx on Docker, then back to Tomcat running on our localhost, and Tomcat connects to a database also running in Docker. I have got this working on my linux box using the ‘–add-host’ argument so Nginx can see my host, however my Mac friends using boot2docker can’t use this approach because the host for them is a Virtualbox VM.

    Any pointers?

  3. 0.11.5-M1 indeed has a bug which caused this NPE. 0.11.5 is out since last week and contains a fix for this.

    @arun, maybe you could update your post and exchange 0.11.5-M1 with 0.11.5 ?

    Thanks …
    … roland

  4. Thanks Arun, can you tell me if you run your Java stuff locally on your Mac in your IDE? Or do you run a Docker container which has your web app server (Tomcat, JBoss, whatever) and use volumes to access your code and then run your app server locally in your IDE? We’re just having trouble finding an existing solution on this and would be surprised if we are the first to have run into it.

  5. Collin,

    All my Java stuff runs locally on my laptop. Have been meaning to play with reproducible environments with Docker though, so hopefully in the next few weeks.

  6. Pingback: Java EE Deployment Scenarios for Docker Containers | Social Marketing by WordPress

Leave a Reply

Your email address will not be published. Required fields are marked *