Docker quickly deploys springboot project

Many developers will encounter some cumbersome problems during project deployment, such as packaging, uploading, and deployment. Using Docker can solve these problems very conveniently. In this article, I will explain in detail how to use the docker packaging plug-in in IDEA to package the code and publish it directly to the server. In this way, we can complete the deployment of the project very quickly. Of course, you may have questions, namely: how to deploy the code I wrote in IDEA to the server? Below, we describe the deployment process in detail.

1. Configure docker remote access

This step is very critical, and it is an important step for our IDEA to associate with the server:

1. Modify /lib/systemd/system/docker.service

vim /lib/systemd/system/docker.service

Modify the startup options of the Docker daemon so that it can listen to the Docker API request from the remote client. The -H parameter is used to specify the address and port to listen to. Here, tcp://0.0.0.0:2375 is used to indicate the 2375 that listens to all network interfaces. port.

//将
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
//替换为
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock

2. Restart docker

systemctl daemon-reload
systemctl restart docker

3. Test whether the modification is successful

http://IP:2375/version

2. Configure in IDEA

1. The specific configuration is as follows:

2. After configuration, docker will appear in the services of IDEA, as shown in the figure below:

3. Configure dockerfile

FROM openjdk
EXPOSE 8044
VOLUME /tmp
ADD shangjian-sap.jar  /shangjian-sap.jar
ENTRYPOINT ["java","-server","-Xms1024m","-Xmx1024m","-Dspring.profiles.active=local","-jar","shangjian-sap.jar"]

The instructions in this Dockerfile are as follows:

  • FROM: Specifies which image our image is based on.
  • EXPOSE: Declare the port that the container needs to listen to when running.
  • COPY: Copies the application's jar file to the container's file system.
  • ENTRYPOINT: Define the command that needs to be run when the container starts, here is to run the jar file.

4. Configure the docker packaging plugin in the pom file

 <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.6.0</version>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
            <!-- 跳过单元测试 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
            <!--使用docker-maven-plugin插件-->
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>1.2.0</version>
                <!--将插件绑定在某个phase执行-->
                <executions>
                    <execution>
                        <id>build-image</id>
                        <!--用户只需执行mvn package ,就会自动执行mvn docker:build-->
                        <phase>package</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <!--指定生成的镜像名-->
                    <imageName>docker/${project.artifactId}</imageName>
                    <!--指定标签-->
                    <imageTags>
                        <imageTag>latest</imageTag>
                    </imageTags>
                    <!-- 指定 Dockerfile 路径-->
                    <dockerDirectory>./</dockerDirectory>
                    <!--指定远程 docker api地址-->
                    <dockerHost>http://自己IP:2375</dockerHost>
                    <!-- 这里是复制 jar 包到 docker 容器指定目录配置 -->
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <!--jar 包所在的路径 此处配置的 即对应 target 目录-->
                            <directory>${project.build.directory}</directory>
                            <!-- 需要包含的 jar包 ,这里对应的是 Dockerfile中添加的文件名 -->
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
        </plugins>
    </build>

5. Package and build image

1. Maven packs and docker builds the image:

2. Create a docker container

step one

step two

step three

step four

6. Summary

In general, this article introduces the method of using the docker plug-in in IDEA to quickly package and deploy projects. This deployment method is suitable for scenarios that require frequent packaging and deployment during the development process. Of course, there are other deployment methods, such as using automated tools such as Jenkins. However, no matter which method is used, the core operation steps are similar: package the project and upload the package to the server, and finally start the project. Depending on the actual situation and needs, choose the deployment method that suits you best.

Guess you like

Origin blog.csdn.net/whc888666/article/details/130368225