Skip to content

Latest commit

 

History

History
124 lines (108 loc) · 3.95 KB

docker-maven.adoc

File metadata and controls

124 lines (108 loc) · 3.95 KB

Docker Maven Plugin

Maven plugin allows you to manage Docker images and containers from 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

Run Java EE Application

  1. Clone the workspace as:

    git clone https://github.com/javaee-samples/javaee7-docker-maven.git
  2. Build the image as:

    mvn package -Pdocker
  3. Verify the image as:

    docker images
    REPOSITORY                        TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
    arungupta/javaee7-docker-maven    latest              2e51b3fca40f        4 seconds ago       581.5 MB
  4. Run the container as:

    mvn install -Pdocker
  5. Access your application at http://dockerhost:8080/javaee7-docker-maven/resources/persons. It shows the output as:

    curl http://dockerhost:8080/javaee7-docker-maven/resources/persons
    <?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>

Understand Plugin Configuration

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

<plugin>
    <groupId>org.jolokia</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>0.11.5</version>
    <configuration>
        <images>
            <image>
                <alias>user</alias>
                <name>arungupta/javaee7-docker-maven</name>
                <build>
                    <from>arungupta/wildfly:8.2</from>
                    <assembly>
                        <descriptor>assembly.xml</descriptor>
                        <basedir>/</basedir>
                    </assembly>
                    <ports>
                        <port>8080</port>
                    </ports>
                </build>
                <run>
                    <ports>
                        <port>8080:8080</port>
                    </ports>
                </run>
            </image>
        </images>
    </configuration>
    <executions>
        <execution>
            <id>docker:build</id>
            <phase>package</phase>
            <goals>
                <goal>build</goal>
            </goals>
        </execution>
        <execution>
            <id>docker:start</id>
            <phase>install</phase>
            <goals>
                <goal>start</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Each image configuration has three parts:

  1. Image name and alias

  2. <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 the src/main/docker directory.

    assembly.xml in our case looks like:

    <assembly . . .>
      <id>javaee7-docker-maven</id>
      <dependencySets>
        <dependencySet>
          <includes>
            <include>org.javaee7.sample:javaee7-docker-maven</include>
          </includes>
          <outputDirectory>/opt/jboss/wildfly/standalone/deployments/</outputDirectory>
          <outputFileNameMapping>javaee7-docker-maven.war</outputFileNameMapping>
        </dependencySet>
      </dependencySets>
    </assembly>
  3. <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.