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
spotifyand
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.
<code><plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
...
</plugin>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
...
</plugin>
</code>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.
<code><!-- configuration via XML ... -->
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
</plugin>
mvn jib:dockerBuild
</code>Problems 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.10and publishing to the registry at
192.168.0.20.
<code><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>
</code>Run
mvn spring-boot:build-imageto 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.
<code>echo $DOCKER_HOST
tcp://172.17.0.111:2375
</code>Network support
During build, large dependencies (~100 MB) are downloaded from GitHub and may fail; use a proxy or an overseas ECS to resolve.
<code>:: 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 SUCCESS
</code>References
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
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.