Docker's one click deployment method of SpringBoot application is quick and easy for thieves to use!

Posted by thaynejo on Sat, 19 Feb 2022 16:56:43 +0100

stay Gradle can really kill Maven? Today I experienced it, thief cool In this article, we talked about using Gradle to build SpringBoot applications. These two days, we found another Gradle plug-in that supports one click packaging and push Docker images. Today, let's talk about this plug-in. I hope it will be helpful to you!

SpringBoot e-commerce project mall (40k+star) address: https://github.com/macrozheng/mall

Introduction to Gradle Docker Plugin

A plug-in that can manage Docker images and containers through remote API s. It is designed for Java applications and supports SpringBoot natively.

The plug-in has the following features:

  • Seamless integration with the build tool Gradle and its DSL.
  • Handle the complex communication logic between Docker client and daemon behind the scenes.
  • Simplify the definition of complex workflow.
  • Minimize the writing logic of build scripts.

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

  • com. bmuschko. Docker remote API: provides customized tasks, which can interact with docker through remote API.
  • com. bmuschko. Docker Java application: create and push docker images for Java applications.
  • com. bmuschko. Docker spring boot application: create and push docker images for spring boot applications.

Operation mirror

Take my scaffolding project mall tiny as an example. Let's see if it's fast and easy to use this plug-in to package and push Docker images!

Build image

  • To use the plug-in, we need to build The following configurations are made in gradle. Here, choose to use the remote API plug-in and SpringBoot plug-in;
plugins {
    id 'com.bmuschko.docker-remote-api' version '6.7.0'
    id 'com.bmuschko.docker-spring-boot-application' version '6.7.0'
}
  • Then, a constant is defined under the ext node, where the address of the image warehouse is defined for reference later;
ext{
    registryUrl='192.168.5.78:5000'
}
  • Next is the very important plug-in configuration. Configure the access path of Docker remote API and the related configuration of SpringBoot application image;
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']
    }
}
  • Next, let's explain the role of these configurations;
attributetypeeffect
urlStringDocker remote API access path
baseImageStringBasic image used by SpringBoot application
maintainerStringProject maintainer
portsListMirror exposed port
imagesSetImage name of package push
jvmArgsListJVM parameters of Java application runtime
  • Next, we can directly use the dockerbuildfimage command in the IDEA to package the application image to the remote server;

  • Let's take a look at the log output from the console. In fact, a Dockerfile is created by default (even the Dockerfile is saved), and then it is used 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-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 in b21d898456cb
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'.
  • The Dockerfile can be found in the build\docker folder of the project. The specific contents are as follows:
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
  • After packaging the image, directly use the following commands to run the project. Pay attention to installing MySQL and Redis.
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

Image push

  • After pushing, you can see the image in our visual image warehouse.

Compare Maven

We can compare the speed of using Gradle and Maven by packaging the project clean into a Docker image.

  • It takes 30 minutes to build the image using Gradle and clean;

  • Using Maven to clean and build a Docker image takes 58s. Sure enough, Gradle is twice as fast as Maven!

summary

Today, we experienced a combination of Gradle and Docker, and found that it was fast and simple enough. Compared with Maven, it is twice as fast and has built-in Dockerfile, which greatly reduces the difficulty of configuration.

reference material

Official documents: https://bmuschko.github.io/gradle-docker-plugin/

Project source code address

https://github.com/macrozheng/mall-tiny/tree/gradle

This article GitHub https://github.com/macrozheng/mall-learning Already included, welcome to Star!

Topics: Java Docker Gradle Maven