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
Copy code
- 1
- 2
- 3
Copy code
- 1
- 2
- 3
3. Remote access test
Copy code
- 1
- 2
- 3
- 4
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
<span class="hljs-tag"><<span class="hljs-name">configuration</span>></span> <span class="hljs-comment"><!-- Image name --></span> <span class="hljs-tag"><<span class="hljs-name">imageName</span>></span>${project.artifactId}<span class="hljs-tag"></<span class="hljs-name">imageName</span>></span> <span class="hljs-comment"><!-- Specify label --></span> <span class="hljs-tag"><<span class="hljs-name">imageTags</span>></span> <span class="hljs-tag"><<span class="hljs-name">imageTag</span>></span>latest<span class="hljs-tag"></<span class="hljs-name">imageTag</span>></span> <span class="hljs-tag"></<span class="hljs-name">imageTags</span>></span> <span class="hljs-comment"><!-- base image --></span> <span class="hljs-tag"><<span class="hljs-name">baseImage</span>></span>openjdk:8-jdk-alpine<span class="hljs-tag"></<span class="hljs-name">baseImage</span>></span> <span class="hljs-comment"><!-- Switch to container working directory--></span> <span class="hljs-tag"><<span class="hljs-name">workdir</span>></span>/ROOT<span class="hljs-tag"></<span class="hljs-name">workdir</span>></span> <span class="hljs-tag"><<span class="hljs-name">entryPoint</span>></span>["java","-jar","${project.build.finalName}.jar"]<span class="hljs-tag"></<span class="hljs-name">entryPoint</span>></span> <span class="hljs-comment"><!-- Specify remote Docker API address --></span> <span class="hljs-tag"><<span class="hljs-name">dockerHost</span>></span>http://192.168.1.111:2375<span class="hljs-tag"></<span class="hljs-name">dockerHost</span>></span> <span class="hljs-comment"><!-- copy jar Package to docker Container specified directory--></span> <span class="hljs-tag"><<span class="hljs-name">resources</span>></span> <span class="hljs-tag"><<span class="hljs-name">resource</span>></span> <span class="hljs-tag"><<span class="hljs-name">targetPath</span>></span>/ROOT<span class="hljs-tag"></<span class="hljs-name">targetPath</span>></span> <span class="hljs-comment"><!-- Used to specify the root directory to be copied, ${project.build.directory}express target catalogue --></span> <span class="hljs-tag"><<span class="hljs-name">directory</span>></span>${project.build.directory}<span class="hljs-tag"></<span class="hljs-name">directory</span>></span> <span class="hljs-comment"><!-- Used to specify the files to be copied, ${project.build.finalName}.jar It's packed target Under directory jar Package name --></span> <span class="hljs-tag"><<span class="hljs-name">include</span>></span>${project.build.finalName}.jar<span class="hljs-tag"></<span class="hljs-name">include</span>></span> <span class="hljs-tag"></<span class="hljs-name">resource</span>></span> <span class="hljs-tag"></<span class="hljs-name">resources</span>></span> <span class="hljs-tag"></<span class="hljs-name">configuration</span>></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).