Effortless Docker Image Creation for Spring Boot: Maven Plugins vs Jib
This article compares Maven Docker plugins (spotify, fabric8) and Jib, highlights their drawbacks for Spring Boot projects, and shows how Spring Boot 2.4's built‑in docker support simplifies image building and publishing via the spring‑boot‑maven‑plugin.
Background
During development we often use Maven to compile and package, then generate Docker images, which improves deployment speed, scaling, and rollback. The docker‑maven‑plugin helps automatically build and push images from a Maven project.
spotify, fabric8
Two common plugins are spotify and fabric8, which define a Dockerfile via XML or mount an external Dockerfile and invoke the Docker remote API to build the image.
pig micro‑service platform [1] is the basis for all containerization.
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
...
</plugin>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
...
</plugin>Run the plugin lifecycle with mvn docker:build && mvn docker:push.
jib
When code changes are small, using the previous plugins rebuilds the whole image, wasting storage and bandwidth.
Jib, released by Google in July 2018, builds Java images (Maven/Gradle) by reusing build cache, speeding up builds and reducing image size.
<!-- configuration via XML ... -->
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
</plugin>
mvn jib:dockerBuildProblems with the three approaches
Most Spring Boot projects share the same Dockerfile; redefining it via XML or external Dockerfile is unnecessary.
The plugins require knowledge of Dockerfile syntax, which is not developer‑friendly.
Spring Boot 2.3+ provides layered JARs, reducing the need for custom Dockerfiles.
Solution
Spring Boot 2.4 introduces its own Docker build support integrated into spring-boot-maven-plugin. Configuring the target registry and host is enough to build the image.
With the configuration below, the image can be built on a development machine without Docker installed, using Docker Remote API at 192.168.0.10 and publishing to the registry at 192.168.0.20.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<image>
<name>192.168.0.20/pig4cloud/${project.artifactId}</name>
<publish>true</publish>
</image>
<docker>
<host>http://192.168.0.10:2375</host>
<tlsVerify>false</tlsVerify>
<publishRegistry>
<username>username</username>
<password>password</password>
<url>192.168.0.20</url>
</publishRegistry>
</docker>
</configuration>
</plugin>Run mvn spring-boot:build-image to build and automatically push the image.
Other notes
Docker host configuration not effective
Even after setting the node in the UI, an error shows host mismatch; check the $DOCKER_HOST environment variable, which takes precedence.
echo $DOCKER_HOST
tcp://172.17.0.111:2375Network support
During build, large dependencies (~100 MB) are downloaded from GitHub and may fail; use a proxy or an overseas ECS to resolve.
:: Spring Boot :: (v2.4.0)
[INFO] > Running creator
[INFO] [creator] Downloading from https://github.com/bell-sw/Liberica/...
[INFO] [creator] JVMKill Agent 1.16.0: Contributing to layer
[INFO] [creator] Downloading from https://github.com/cloudfoundry/jvmkill/...
[INFO] [creator] Downloading from https://repo.spring.io/release/...
[INFO] Verifying checksum
[INFO] 192.168.0.20/pig4cloud/demo:latest
[INFO] Successfully built image '192.168.0.20/pig4cloud/demo:latest'
[INFO] > Pushing image '192.168.0.20/pig4cloud/demo:latest' 100%
[INFO] > Pushed image '192.168.0.20/pig4cloud/demo:latest'
[INFO] BUILD SUCCESSReferences
pig micro‑service platform: https://gitee.com/log4j/pig
Spring Boot 2.4 Docker documentation: https://docs.spring.io/spring-boot/docs/2.4.0/maven-plugin/reference/htmlsingle/#build-image-example-publish
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.
Java Architecture Diary
Committed to sharing original, high‑quality technical articles; no fluff or promotional content.
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.
