Over a million developers have joined DZone.

Build Images and Run Docker Containers in Maven

Learn how to use a new Maven plugin to help test Java applications in containers without having to use the error-prone Docker command line.

· DevOps Zone

The DevOps zone is brought to you in partnership with Sonatype Nexus. The Nexus suite helps scale your DevOps delivery with continuous component intelligence integrated into development tools, including Eclipse, IntelliJ, Jenkins, Bamboo, SonarQube and more. Schedule a demo today

In order for Java developers to test their applications in containers they typically have to build their code, create an image, and run a container. You can use the Docker command line interface to manage images and containers, but at least for me that's often error-prone and I have to go back to my cheatsheet to find the right commands. I tried a Docker Maven plugin instead and my initial experience is very positive.

There are actually several Maven plugins for Docker. Roland Huß wrote a nice high level comparison where he explains why he created another plugin. I tried the Spotify one briefly but that one doesn't allow running containers. The plugin from Roland rhuss/docker-maven-plugin seems very promising. Below I used it for my simple Java sample.


With the plugin you can build images and run containers and also easily remove these again which is especially important in the iterative development phase.

Build Image:

> mvn docker:build

Run Container:

> mvn docker:start

Stop and Remove Container:

> mvn docker:stop

Remove Image:

> mvn -Ddocker.removeAll docker:remove

The plugin also supports to push images to other registries but I haven't tried it yet. To find out more read the Intro and the Manual

The DevOps zone is brought to you in partnership with Sonatype Nexus. Use the Nexus Suite to automate your software supply chain and ensure you're using the highest quality open source components at every step of the development lifecycle. Get Nexus today

Topics:
devops ,maven ,docker ,containers

Published at DZone with permission of Niklas Heidloff, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}