Docker Gradle plugin

Even when generating a Docker image, using Docker commands is not hard or complex; it's always a good idea to automate all of these steps as much as we can. The Docker Gradle plugin is pretty useful for accomplishing this task. Let's learn how to make this part of our application.

First of all, we need to include the repository that contains the plugin and the plugin itself as a dependency within the buildscript section, as shown in the following code:

buildscript 
{
...
repositories
{
...
maven
{
url "https://plugins.gradle.org/m2/"
}
}
dependencies
{
...
classpath('gradle.plugin.com.palantir.gradle.docker:gradledocker:
0.13.0')
}
}

Later, the plugin should be applied to the project in the same way as any other plugin—using its ID. This is shown in the following code:

apply plugin: 'com.palantir.docker'

The image build process can be customized using the parameters described in the official documentation at https://github.com/palantir/gradle-docker. To keep things simple, we are only going to indicate the image name that is required within a docker block, as shown in the following code:

docker 
{
name "enriquezrene/spring-boot-${jar.baseName}:${version}"
files jar.archivePath
buildArgs(['JAR_FILE': "${jar.archiveName}"])
}
As you may have noticed, we are now using the variables that are available in the build.gradle file, such as the generated JAR name and its version.

Now that the plugin has been fully integrated within the project, you can build the image by running the following Gradle task:

$ ./gradlew build docker

You can also check the recently created image, as shown in the following screenshot:

The docker images console output

It is a good idea to have all of these steps automated as this provides free documentation that can be improved in the future if needed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.244.250