Docker practice | Part 3: IDEA integrates docker plug-in to realize one click automatic packaging and deployment of microservice projects

1, Foreword

When you play microservice projects, you often have more than a dozen services. Each modification and deployment one by one is cumbersome and will waste more and more time. Therefore, this article arranges and realizes the one-click deployment of microservices through one-time configuration to realize the so-called once and for all.

2, Configure server

1. Docker installation

The server needs to install Docker. If it is not installed, please refer to this article for installation Docker practice | Part 1: installing docker on Linux

2. Docker enables remote access

  
Copy code
  • 1
  • 2
  • 3
nginx
vim /usr/lib/systemd/system/docker.service #Append in ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock

  
Copy code
  • 1
  • 2
  • 3
properties
#Restart systemctl daemon-reload systemctl restart docker
  
Copy code
  • 1
  • 2
  • 3
dockerfile
#Open 2375 port firewall-cmd --zone=public --add-port=2375/tcp --permanent firewall-cmd --reload

3. Remote access test

  
Copy code
  • 1
  • 2
  • 3
  • 4
nginx
#Check whether port listening is enabled netstat -nlpt #Does the curl test take effect curl http://127.0.0.1:2375/info

2, Configure IDEA

Install Docker plug-in in IDEA and open the plug-in market (file - > Settings - > plugins)

After installing the Docker plug-in, configure the Docker remote link

3, Build Docker image with Maven plug-in

1. Configure pom.xml

  
Copy code
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
xml
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>1.0.0</version> <executions> <!-- Execute mvn package, that is, execute MVN clean package docker: build -- > <execution> <id>build-image</id> <phase>package</phase> <goals> <goal>build</goal> </goals> </execution> </executions>
<span class="hljs-tag">&lt;<span class="hljs-name">configuration</span>&gt;</span>
    <span class="hljs-comment">&lt;!-- Image name --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">imageName</span>&gt;</span>${project.artifactId}<span class="hljs-tag">&lt;/<span class="hljs-name">imageName</span>&gt;</span>
    <span class="hljs-comment">&lt;!-- Specify label --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">imageTags</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">imageTag</span>&gt;</span>latest<span class="hljs-tag">&lt;/<span class="hljs-name">imageTag</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">imageTags</span>&gt;</span>
    <span class="hljs-comment">&lt;!-- base image --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">baseImage</span>&gt;</span>openjdk:8-jdk-alpine<span class="hljs-tag">&lt;/<span class="hljs-name">baseImage</span>&gt;</span>

    <span class="hljs-comment">&lt;!-- Switch to container working directory--&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">workdir</span>&gt;</span>/ROOT<span class="hljs-tag">&lt;/<span class="hljs-name">workdir</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">entryPoint</span>&gt;</span>["java","-jar","${project.build.finalName}.jar"]<span class="hljs-tag">&lt;/<span class="hljs-name">entryPoint</span>&gt;</span>

    <span class="hljs-comment">&lt;!-- Specify remote Docker API address  --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">dockerHost</span>&gt;</span>http://192.168.1.111:2375<span class="hljs-tag">&lt;/<span class="hljs-name">dockerHost</span>&gt;</span>

    <span class="hljs-comment">&lt;!-- copy jar Package to docker Container specified directory--&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">resources</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">resource</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">targetPath</span>&gt;</span>/ROOT<span class="hljs-tag">&lt;/<span class="hljs-name">targetPath</span>&gt;</span>
            <span class="hljs-comment">&lt;!-- Used to specify the root directory to be copied, ${project.build.directory}express target catalogue --&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">directory</span>&gt;</span>${project.build.directory}<span class="hljs-tag">&lt;/<span class="hljs-name">directory</span>&gt;</span>
            <span class="hljs-comment">&lt;!-- Used to specify the files to be copied, ${project.build.finalName}.jar It's packed target Under directory jar Package name --&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">include</span>&gt;</span>${project.build.finalName}.jar<span class="hljs-tag">&lt;/<span class="hljs-name">include</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">resource</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">resources</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">configuration</span>&gt;</span>

</plugin>

2. Maven packaging and image making

The project is an aggregation project. First, execute mvn install -DskipTests=true globally to install the module jar package to the local warehouse, otherwise the dependencies between modules will report errors.

Switch to the folder Youlai gateway, execute the project packaging mvn package -DskipTests=true, and complete the image generation in the package life cycle.

3. IDEA creates and starts containers

4. Vessel start-up test

4, Conclusion

This article aims to use IDEA and Docker plug-in to quickly build Docker image and realize one click deployment of SpringBoot project. There are two methods mentioned in this article. Although Docker made plugin has been officially abandoned, it is deployed here youlai-mall Using DockerFile Maven is better than using DockerFile maven, because in addition to configuring pom.xml and writing DockerFile files, DockerFile Maven also configures system environment variables. If you have more than one server, how to handle the environment variables and cut them around? Therefore, how to choose these two plug-ins really depends on their own use scenarios, and we can't blindly follow others. In particular, the next article will introduce how to avoid the exposure of Docker remote 2375 port to mining, which can better reflect the advantages of Docker Maven plugin when making changes.

Finally, I would like to recommend my own open source projects youlai-mall , a complete OAuth2 unified authentication full stack mall system with front-end and back-end separation + micro services. Interested friends can contact me, which can give you a good opportunity to improve your experience and ability in open source projects. Micro signal (Haoxian Rui).

Tags: Docker IDEA

Posted on Mon, 04 Oct 2021 14:06:08 -0400 by dandare