SpringBoot e-commerce project mall (40K + STAR) address: github.com/macrozheng/…

Abstract

In Gradle, can You really kill Maven? Gradle is a Gradle plugin that supports one-click packaging and push Docker images. Today we are going to talk about this plugin, hope to help you!

Gradle Docker Plugin

Docker images and containers can be managed by remote API plugin, designed for Java applications, native support for SpringBoot.

Using this plug-in has the following features:

  • Seamlessly integrates with the build tool Gradle and its DSL.
  • Handles the complex communication logic between Docker clients and daemons behind the scenes.
  • Simplifies the definition of complex workflows.
  • Minimize the writing logic of build scripts.

The plug-in consists of the following three plug-ins:

  • com.bmuschko.docker-remote-api: Provides custom tasks that can interact with Docker via a remote API.
  • com.bmuschko.docker-java-application: Creates and pushes Docker images for Java applications.
  • com.bmuschko.docker-spring-boot-application: Creates and pushes a Docker image for the SpringBoot application.

Operating the mirror

Using my scaffolding project Mall-Tiny as an example, let’s see if it’s quick and easy to package push Docker images with this plugin!

Build the mirror

  • To use the plugin, we need to use thebuild.gradleThe remote API plug-in and SpringBoot plug-in are selected.
plugins {
    id 'com.bmuschko.docker-remote-api' version '6.7.0'
    id 'com.bmuschko.docker-spring-boot-application' version '6.7.0'
}
Copy the code
  • Then, inextA constant is defined below the node, where the mirror warehouse address is defined for later reference;
ext{
    registryUrl='192.168.5.78:5000'
}
Copy the code
  • Next is the very important plug-in configuration, configure the Docker remote API access path, and SpringBoot application image related configuration;
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']}}Copy the code
  • Let’s look at what these configurations actually do;
attribute type role
url String Docker remote API access path
baseImage String The base image used by SpringBoot applications
maintainer String Project maintainer
ports List The port exposed by the mirror
images Set The image name of the package push
jvmArgs List JVM parameters for the Java application runtime
  • Next we use it directly in IDEAdockerBuildImageCommand to package the application image to a remote server.

  • Create a Dockerfile by default, and then use it to package the Docker 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 - the SNAPSHOT'.
Step 1/8 : FROM java:8
 ---> d23bdf5b1b1b
Step 2/8 : LABEL maintainer=macrozheng
 ---> Running in 9a63f56a03ae
Removing intermediate container 9a63f56a03ae
 ---> ed45af8fff90
Step 3/8 : WORKDIR /app
 ---> Running in 8bd4b513eb23
Removing intermediate container 8bd4b513eb23
 ---> d27759d1d7df
Step 4/8 : COPY libs libs/
 ---> 84c3a983972a
Step 5/8 : COPY resources resources/
 ---> c8a27f3475fc
Step 6/8 : COPY classes classes/
 ---> 3a76a8efc02b
Step 7/8 : ENTRYPOINT ["java"."-Dspring.profiles.active=prod"."-cp"."/app/resources:/app/classes:/app/libs/*"."com.macro.mall.tiny.MallTinyApplication"]
 ---> Running in e56ae56fd6eb
Removing intermediate container e56ae56fd6eb
 ---> 22d73f95e756
Step 8/8 : EXPOSE 8080
 ---> Running inb21d898456cb Removing intermediate container b21d898456cb ---> 73684cf8c643 Successfully built 73684cf8c643 Successfully Tagged 192.168.5.78:5000/mall-tiny/mall-tiny: 1.0.0-snapshot Created image with ID'73684cf8c643'.

BUILD SUCCESSFUL in 34s
5 actionable tasks: 5 executed
10:56:15: Task execution finished 'dockerBuildImage'.
Copy the code
  • In the projectbuild\dockerThe Dockerfile can be found in the folder.
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
Copy the code
  • After packing the image, you can run the project directly by using the following command. Note that MySQL and Redis are installed.
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 - the SNAPSHOTCopy the code

Push the mirror

  • Next, let’s try the push image function, but first we need to install a visual image repository, see “Still manually deploying the SpringBoot application? Try this automation plug-in!” ;

  • Push image is also very simple, directly in the IDEA using the dockerPushImage command can be;

  • After the push is complete, the image can be seen in our visual image repository.

Contrast the Maven

We compared the speed of using Gradle and Maven by packaging the project Clean later as a Docker image.

  • Using Gradle to clean and build Docker images takes time30s;

  • Using Maven to clean and build Docker images takes time58sGradle is still twice as fast as Maven.

conclusion

Today we tried using Gradle and Docker together and found it is really quick and easy. It is twice as fast as Maven and has built-in Dockerfile, greatly reducing the configuration difficulty.

The resources

The official document: bmuschko. Making. IO/gradle – the dock…

Project source code address

Github.com/macrozheng/…

In this paper, making github.com/macrozheng/… Already included, welcome everyone Star!