Java EE applications are typically built and packaged using Maven. For example, 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: Introduction provides a high level introduction to the plugin including building images, running containers and configuration. Run Java EE 7 Application as a Docker Container using Maven TLDR;

  • Create and Configure a Docker Machine as explained in Docker Machine to Setup Docker Host
  • Clone the workspace as: `git clone`
  • Build the Docker image as: `mvn package -Pdocker`
  • Run the Docker container as: `mvn install -Pdocker`
  • Find out IP address of the Docker Machine as: `docker-machine ip mydocker`
  • Access your application

javaee7-docker-maven> curl
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><collection><person><name>Penny</name></person><person><name>Leonard</name></person><person><name>Sheldon</name></person><person><name>Amy</name></person><person><name>Howard</name></person><person><name>Bernadette</name></person><person><name>Raj</name></person><person><name>Priya</name></person></collection>

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:
    <assembly . . .>
  • `<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 best serves your purpose. How are you creating your Docker images from existing applications?

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

Running Java EE Applications as Docker Containers Using Maven

| Cloud| 2,485 views | 0 Comments
About The Author
- Arun Gupta is the vice president of developer advocacy at Couchbase. He has been building developer communities for 10+ years at Sun, Oracle, and Red Hat. He has deep expertise in leading cross-functional teams to develop and execute strategy, planning and execution of content, marketing campaigns, and programs. Prior to that he led engineering teams at Sun and is a founding member of the Java EE team. Gupta has authored more than 2,000 blog posts on technology. He has extensive speaking experience in more than 40 countries on myriad topics and is a JavaOne Rock Star. Gupta also founded the Devoxx4Kids chapter in the US and continues to promote technology education among children. An author of a best-selling book, an avid runner, a globe trotter, a Java Champion, and a JUG leader, he is easily accessible at @arungupta.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>