Cloud Native 8 min read

Build and Push SpringBoot Docker Images with Gradle in Minutes

This article introduces the Gradle Docker plugin, shows how to configure it for a SpringBoot project, demonstrates building and pushing Docker images directly from Gradle, and compares the build speed with Maven, highlighting the simplicity and speed gains.

macrozheng
macrozheng
macrozheng
Build and Push SpringBoot Docker Images with Gradle in Minutes

Gradle Docker Plugin Overview

The Gradle Docker plugin provides remote‑API management of Docker images and containers, designed for Java applications and natively supporting SpringBoot.

Seamless integration with Gradle DSL.

Handles complex communication between Docker client and daemon.

Simplifies workflow definitions.

Minimizes build‑script logic.

The plugin consists of three parts: com.bmuschko.docker-remote-api: custom tasks for remote Docker API interaction. com.bmuschko.docker-java-application: creates and pushes Docker images for Java applications. com.bmuschko.docker-spring-boot-application: creates and pushes Docker images specifically for SpringBoot applications.

Building an Image

Using the sample project mall-tiny, add the following plugins to build.gradle:

plugins {
    id 'com.bmuschko.docker-remote-api' version '6.7.0'
    id 'com.bmuschko.docker-spring-boot-application' version '6.7.0'
}

Define the registry URL:

ext {
    registryUrl = '192.168.5.78:5000'
}

Configure the Docker remote API and SpringBoot image settings:

docker {
    url = 'tcp://192.168.5.78:2375'
    springBootApplication {
        baseImage = 'java:8'
        maintainer = 'macrozheng'
        ports = [8080]
        images = ["${registryUrl}/mall-tiny/${rootProject.name}:${version}"]
        jvmArgs = ['-Dspring.profiles.active=prod']
    }
}

Running the dockerBuildImage task generates a Dockerfile automatically and builds the image:

> Task :dockerBuildImage
Building image using context 'I:\developer\gitee\mall-tiny-gradle\build\docker'.
Using images '192.168.5.78:5000/mall-tiny/mall-tiny:1.0.0-SNAPSHOT'.
... (build log omitted) ...
Successfully built 73684cf8c643
Successfully tagged 192.168.5.78:5000/mall-tiny/mall-tiny:1.0.0-SNAPSHOT
BUILD SUCCESSFUL in 34s

The generated Dockerfile (located in build/docker) looks like:

FROM java:8
LABEL maintainer=macrozheng
WORKDIR /app
COPY libs libs/
COPY resources resources/
COPY classes classes/
ENTRYPOINT ["java","-Dspring.profiles.active=prod","-cp","/app/resources:/app/classes:/app/libs/*","com.macro.mall.tiny.MallTinyApplication"]
EXPOSE 8080

Run the container with:

docker run -p 8080:8080 --name mall-tiny \
--link mysql:db \
--link redis:redis \
-v /etc/localtime:/etc/localtime \
-v /mydata/app/mall-tiny/logs:/var/logs \
-d 192.168.5.78:mall-tiny/mall-tiny:1.0.0-SNAPSHOT

Pushing the Image

After installing a visual Docker registry, use the dockerPushImage task in IDEA to push the image. The pushed image appears in the registry UI.

Comparison with Maven

Cleaning and building the Docker image with Gradle takes about 30s, while the same process with Maven takes roughly 58s, demonstrating that Gradle can be twice as fast.

Conclusion

Integrating Gradle with Docker provides a fast and simple workflow for building and publishing SpringBoot Docker images, automatically generating Dockerfiles and offering significant speed advantages over Maven.

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.

JavaDockerci/cdGradlecontainerizationSpringBoot
macrozheng
Written by

macrozheng

Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.

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.