Are you still manually packaging the SpringBoot project, uploading it to the server, and manually executing startup commands to start the project? You out! Through Docker configuration DockerMaven plug-in, rapid deployment, one-click deployment of springboot server to the server, one-click startup, farewell to the traditional deployment mode, the first step to achieve automatic operation and maintenance, cardiac? Take a look
Microservice deployment mode
(1) Manual deployment: First generate JAR package (or WAR package) based on source package, upload jar package (or WAR package) to virtual machine and copy to JDK container.
(2) Automatic deployment through Maven plug-in.
Manual deployment can be cumbersome and error-prone for a large number of microservices. So here we learn how to automate deployment, which is often used in real enterprise development.
Maven plug-in automatic deployment steps:
(1) Modify the docker configuration of the host so that it can be accessed remotely
vi /lib/systemd/system/docker.service
Copy the code
Including ExecStart = add configuration – H after TCP: / / 0.0.0.0:2375 – H Unix: / / / var/run/docker. The sock
Note: For a cloud server, after port 2375 is enabled, you need to enable port 2375 in the security group in the corresponding server console. Otherwise, the Internet is still inaccessible.
In addition: TCP ://0.0.0.0:2375 this specifies that all IP can access the server 2375 port, cloud server will definitely be mined! (hands-on), so it is best to specify your own fixed IP, such as TCP ://192.168.1.1:2375
(2) Refresh the configuration and restart the service
systemctl daemon-reload
systemctl restart docker
docker start registry
Copy the code
(3) Add the DockerMaven plugin configuration
The DockerMaven plugin can be configured in either of two ways:
- The first wayIn:
pom.xml
In the
<build>
<finalName>app</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<! - the docker maven plug-ins, website: https://github.com/spotify/docker-maven-plugin - >
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<configuration>
<! - 192.168.1.1:5000 configuration is docker private IP and port of the warehouse, you can change for their own, said the use of mirror for where -- -- >
<imageName>192.168.1.1, 5000 / ${project. ArtifactId} : ${project. Version}</imageName>
<! -- the name of the mirror used -->
<baseImage>jdk1.8</baseImage>
<! -- Command executed in the image, that is, start the Springboot project -->
<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
<! -- dockerhost specifies the server where the image was uploaded -->
<dockerHost>http://192.168.1.1:2375</dockerHost>
</configuration>
</plugin>
</plugins>
</build>
Copy the code
The above configuration will automatically generate a Dockerfile:
FROM jdk1.8 ADD app.jar / ENTRYPOINT [“java”,”-jar”,”/app.jar”]
-
Second way :(custom Dockerfile)
pom.xml
** Note: ** is not quite the same as the first method
<build> <finalName>app</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <! -- Plugin ::plugin[] Maven plugin for docker --> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.3</version> <configuration> <! -- Specify the name of the image to be generated --> <imageName>${project.build.finalName}/${project.artifactId}:${project.version}</imageName> <dockerDirectory>src/main</dockerDirectory> <dockerHost>http://47.98.232.143:12375</dockerHost> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> <! -- end::plugin[] --> </plugins> </build> Copy the code
This does not specify where to use the image, i.e. does not specify the configuration of Dockerfile
-
Dockerfile configuration
To place a Dockerfile in the resources directory
# Based on which mirror FROM java:8 Mount the local folder to the current container VOLUME /tmp Tensquare_blog 0.0.1- snapshot. jar = tensquare_blog 0.0.1- snapshot. jar = tensquare_blog 0.0.1- snapshot. jar = tensquare_blog 0.0.1 ADDTensquare_blog - 1.0 - the SNAPSHOT. Jar app. The jar RUN bash -c 'touch /app.jar' # Configure time zone ENV TZ=Asia/Shanghai RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone Configure the commands to be executed after the container is started If not, delete the last profile ENTRYPOINT ["java"."-Djava.security.egd=file:/dev/./urandom"."-jar"."/app.jar"."--spring.profiles.active=production"] Expose the specified port, the exposed port of the SpringBoot project EXPOSE 9002 Copy the code
(4) In the Windows command prompt, go to the project directory
- Installation required dependencies
mvn install
-
Run the docker plug-in command to upload the image
Go to the directory where the service resides (in the subproject under the parent project) and run the following command to package and upload the image
mvn docker:build -DpushImage
(5) Enter the host computer and view the image
docker images
If you can view the uploaded image, the upload is complete
(6) Start the service
Docker run-di –name=tensquare_blog -p 9002:9002 tensquare_blog -1.0-snapshot.jar docker run-di –name=tensquare_blog -p 9002:9002 tensquare_blog -1.0-snapshot.jar
Start the service
(7) View startup logs
docker logs --tail 300 -f
Viewing Console Logs
Of course, Jenkins can automate the building and publishing of microservices, making them more automated and more powerful with just a few clicks! We will introduce ~~ later
Welcome to the wechat public account “code on the finger tip”
Original is not easy, click a praise and then go ~ welcome to pay attention to, to bring you more wonderful articles!
Your likes and attention are the biggest motivation for writing articles