Spring bolt is packaged into Docker images in two ways
- Turn the springboot project into a jar package, upload it to the specified directory of the linux server, and then write a Dockefile file in the corresponding directory, and then package it
- IDEA integrates docker plug-in, which is remotely packaged as an image to linux through maven's docker plug-in, and can operate containers and images in the native IDEA
The second method is described below, not the first
Step 1: configure remote access of Docker
1. Modify / usr/lib/systemd/system/docker.service
vim /usr/lib/systemd/system/docker.service
2. Replace ExecStart
take ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock Replace with ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock
3. Restart Docker service
systemctl daemon-reload systemctl restart docker
4. Check whether port 2375 is listening
netstat -nlpt

5. The server firewall opens port 2375
firewall-cmd --add-port=2375/top --permanent firewall-cmd --reload firewall-cmd --zone=public --list-ports
6. Check whether it is configured properly
linux host ip plus 2375 port number / version. If something comes out, it indicates that the configuration is successful

IDEA configuration
Install the docker plug-in and set the address of the remote docker

Add Maven docker plug-in to pom.xml to automatically generate images and push them to the warehouse
If you use the made docker plug-in, you can choose to specify the file location of dockerfile in the configuration tab, or you can configure an alternative dockerfile in the configuration tab
The first of the following is to specify the location of the dockfile file:
<plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>1.0.0</version> <!--Make relevant configuration--> <configuration> <!-- appoint Dockerfile route ${project.basedir}: Under the project root path--> <dockerDirectory>${project.basedir}</dockerDirectory> <!--Specifies the name of the generated image: prefix/entry name--> <imageName>xfx/${project.artifactId}</imageName> <!--Specify label--> <imageTags> <imageTag>latest</imageTag> </imageTags> <!-- long-range docker Address of--> <dockerHost>Http://192.168.112.128:2375</dockerHost> <!-- Here is replication jar Package to docker Container specified directory configuration --> <resources> <resource> <targetPath>/</targetPath> <!--jar The path where the package is located corresponds to the path configured here target catalogue--> <directory>${project.build.directory}</directory> <!-- Need to include jar Package, here corresponds to Dockerfile File name added in --> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin>
Corresponding Dockerfile
#The basic image is created through Java 8 FROM java:8 #Mount volumes are temporary directories VOLUME /tmp #Copy demo1-520.finally.jar.jar to demo1-520.finally.jar.jar in the container (this demo1-520.finally.jar.jar is self generated) COPY target/Demo1-520.finnally.jar /xfx.jar #Exposed port EXPOSE 80 #Execute the command Java - jar ENTRYPOINT ["java","-jar","/xfx.jar"]
Here, the relative path of COPY starts from the directory where the current Dockerfile is located
COPY target/Demo1-520.finnally.jar /xfx.jar

Completely replace dockerfile file
<properties> <java.version>1.8</java.version> <docker.image.prefix>xfx</docker.image.prefix> </properties> <build> <plugins> <plugin> <!-- springboot Used to generate jar Package plug-ins--> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>1.0.0</version> <!-- configuration Alternative written Dockerfile file --> <configuration> <!-- Specify base mirror--> <baseImage>java</baseImage> <!-- Creator's own information--> <maintainer>dhy dhy@qq.com</maintainer> <!-- Switch to root catalogue--> <workdir>/ROOT</workdir> <!-- Commands executed--> <cmd>["java","-version"]</cmd> <entryPoint>["java","-jar","${project.build.finalName}.jar"]</entryPoint> <!-- appoint Dcokerfile route,${project.basedir}:Project root path--> <!-- <dockerDirectory>${project.basedir}</dockerDirectory>--> <!--Specifies the name of the generated image: Image name prefix/entry name--> <imageName>${docker.image.prefix}/${project.artifactId}</imageName> <!--Specify label--> <imageTags> <!-- Can be replaced with the version number of the current project--> <imageTag>${project.version}</imageTag> </imageTags> <!-- long-range docker api address--> <dockerHost>Http://192.168.112.128:2375</dockerHost> <!-- Here is replication jar Package to docker Container specified directory configuration --> <resources> <resource> <!-- Under which directory is the container placed--> <targetPath>/ROOT</targetPath> <!-- Specifies the root directory to be replicated--> <!--jar The path where the package is located corresponds to the path configured here target catalogue--> <directory>${project.build.directory}</directory> <!-- Used to specify the files to be copied--> <!-- Need to include jar Package, here corresponds to Dockerfile File name added in --> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> </plugins> </build>
As above, the docker Maven plug-in is used to automatically generate the following files:
FROM java MAINTAINER dhy dhy@qq.com WORKDIR /ROOT ADD /ROOT/Demo1.jar /ROOT/ ENTREPOINT ["java","-jar","Demo1.jar"] CMD ["java","-version"]
Execute mvn command
mvn clean package docke:build Clean up, package, build
Executing the mvn command is a little cumbersome. You can bind the running time of the plug-in to the maven execution packaging life cycle stage
Bind the Docker command to the maven phases
We can divide Docker into build, tag and push, and then bind them to Maven's package and deploy phases respectively
We only need to execute mvn deploy to complete the build,tag and push operations. When we execute mvn build, we only complete the build and tag operations
<executions> <!--When executed mvn package When, execute mvn clean package docker:build--> <execution> <id>build-image</id> <!--Users only need to execute mvn package ,It will be executed automatically mvn docker:build--> <phase>package</phase> <goals> <goal>build</goal> </goals> </execution> <!--When executed mvn package When, the mirror is labeled--> <execution> <id>tag-image</id> <!--Users only need to execute mvn package ,It will be executed automatically mvn docker:tag--> <phase>package</phase> <goals> <goal>tag</goal> </goals> <configuration> <image>${docker.image.prefix}/${project.artifactId}:latest</image> <newName>docker.io/${docker.image.prefix}/${project.artifactId}:${project.version}</newName> </configuration> </execution> <!--When executed mvn deploy When, execute mvn deploy docker:push--> <execution> <id>push-image</id> <!--Users only need to execute mvn package ,It will be executed automatically mvn docker:push--> <phase>deploy</phase> <goals> <goal>push</goal> </goals> </execution> </executions>
Reference articles
One click deployment of Spring Boot to remote Docker container, that's the show!
idea integration docker remote deployment springboot project
(17) Detailed explanation of packaging SpringBoot microservice project into Docker image