Maven plugin allows you to manage Docker images and containers from pom.xml
. It comes with predefined goals:
Goal | Description |
---|---|
|
Create and start containers |
|
Stop and destroy containers |
|
Build images |
|
Push images to a registry |
|
Remove images from local docker host |
|
Show container logs |
-
Clone the workspace as:
git clone https://github.com/javaee-samples/javaee7-docker-maven.git
-
Build the image as:
mvn package -Pdocker
-
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
-
Run the container as:
mvn install -Pdocker
-
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>
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:
-
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 thesrc/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>
-
<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.