Cloud Native 12 min read

Using Maven Docker Plugin to Build, Run, and Push Docker Images for Spring Boot Applications

This tutorial explains how to configure the spotify/docker-maven-plugin in a Spring Boot project's pom.xml, build Docker images with Maven commands, run containers, optionally use a custom Dockerfile, push images to DockerHub, and bind the build goal to the Maven package phase for automated container creation.

Architect's Tech Stack
Architect's Tech Stack
Architect's Tech Stack
Using Maven Docker Plugin to Build, Run, and Push Docker Images for Spring Boot Applications

This article demonstrates how to use the spotify/docker-maven-plugin to build, run, and push Docker images for a Spring Boot application.

Prerequisites include Ubuntu 17.04, Docker 17.12.0‑ce installed, and the plugin available at GitHub .

Simple usage: add the plugin to pom.xml, configure <imageName>, <dockerDirectory>, and resource mapping, then execute mvn clean package docker:build to create the image. Run it with

docker run --name MySpringBootMavenPlugin -d -p 8080:80 docker-spring-boot-demo-maven-plugin

and access the service at http://HOST_IP:8080.

Using a Dockerfile: create a Dockerfile in src/main/resources with FROM java:8, VOLUME /tmp, ADD the built jar, RUN touch /app.jar, EXPOSE 9000, and an ENTRYPOINT. Update the plugin configuration to point <dockerDirectory> to the resources folder and include the jar.

Pushing to DockerHub: add Docker Hub credentials to Maven settings.xml, create a repository on DockerHub, set <imageName> to match the repository, and add <serverId>docker-hub</serverId> and

<registryUrl>https://index.docker.io/v1/</registryUrl>

. Build and push with mvn clean package docker:build -DpushImage.

Binding to a Maven phase: define an <execution> with <phase>package</phase> and <goal>build</goal> so that mvn package automatically builds the Docker image.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

ci/cdcloud-nativeContainerspring-boot
Architect's Tech Stack
Written by

Architect's Tech Stack

Java backend, microservices, distributed systems, containerized programming, and more.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.