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.
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-pluginand 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Architect's Tech Stack
Java backend, microservices, distributed systems, containerized programming, and more.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
