The Docker plugin for Gradle provides two main sets of functionality out-of-the-box. It comes with a base plugin for modeling and executing typical Docker commands e.g. for creating an image or starting a container. The base plugin gives you full control over the process you’d like to define.

Additionally, it ships with a convenience abstraction for packaging Java applications as a Docker image. Unfortunately, the Java application abstraction won’t quite work for building Spring Boot applications. In the future, I might implement an abstraction suited for this specific use case. For now, we’ll just use the most basic functionality giving us the most freedom in defining the process we need.

Getting started with the Docker plugin is straightforward. You just have to apply the plugin with the identifier com.bmuschko.docker-remote-api and the concrete version. Furthermore, you will also have to provide the credentials for the registry we’ll want to use for hosting our image. As you can see in listing 1, the credentials have not been hard-coded in the build script. They can either be provided as environment variables or as project properties (e.g. in a gradle.properties file in your user home directory). In this workflow, we want to push an image to Docker Hub. The Docker plugin uses the Docker Hub registry by default so no additional configuration is required.

build.gradle

plugins { id 'com.bmuschko.docker-remote-api' version '3.2.3' } docker { registryCredentials { username = getConfigurationProperty('DOCKER_USERNAME', 'docker.username') password = getConfigurationProperty('DOCKER_PASSWORD', 'docker.password') email = getConfigurationProperty('DOCKER_EMAIL', 'docker.email') } } String getConfigurationProperty(String envVar, String sysProp) { System.getenv(envVar) ?: project.findProperty(sysProp) }

Listing 1. Configuring the Docker plugin

With this build script, you just layed the ground work for defining the Docker process. As part of the Docker process, we’ll want to achieve the following tasks:

Assemble the Spring Boot application WAR file

Create a Dockerfile containing the instructions for the Docker image

Use the Dockerfile to build an image

Try out the image locally by starting a container to verify that it works as expected

Push the image to Docker Hub