Home >Java >javaTutorial >How does idea quickly package the SpringBoot project into a Docker image and deploy it?

How does idea quickly package the SpringBoot project into a Docker image and deploy it?

WBOY
WBOYforward
2023-05-21 21:28:041734browse

1. Modify the docker configuration file

Modify the file information path as follows:
/etc/docker/daemon.json
Add the following content to the configuration file:

 "hosts": ["tcp://0.0.0.0:2375", "unix:///var/run/docker.sock"]

How does idea quickly package the SpringBoot project into a Docker image and deploy it?

Note: If you don’t have this daemon.json, create one yourself in the /etc/docker/ directory

touch daemon.json

tcp is for remote access, and unix is ​​for local access. If local access is not enabled, the following error will occur when using it on the server:

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running ?

After the modification is completed, execute the following commands in sequence:

#重新加载配置文件                  
systemctl daemon-reload 
# 重启服务 
systemctl restart docker.service 
# 查看端口是否开启 默认端口2375 
netstat -anp|grep 2375

How does idea quickly package the SpringBoot project into a Docker image and deploy it?

How does idea quickly package the SpringBoot project into a Docker image and deploy it?

##2. Configure port opening

Execute the following commands in sequence

添加指定需要开放的端口:
firewall-cmd --zone=public --add-port=2375/tcp --permanent
重载入添加的端口:
firewall-cmd --reload
查询指定端口是否开启成功:
firewall-cmd --query-port=2375/tcp

Note: The above configurations are all performed when the firewall is turned on. If the system firewall is not turned on, the above configuration is not required. For example, my server does not have a firewall configured. Run The following information is displayed:

How does idea quickly package the SpringBoot project into a Docker image and deploy it?

Finally, let’s see if our configuration is effective

curl http://127.0.0.1:2375/info
If the following information appears, the configuration is successful

How does idea quickly package the SpringBoot project into a Docker image and deploy it?

3. IDEA installs the Docker plug-in

If your idea version is higher, the docker plug-in is built-in

How does idea quickly package the SpringBoot project into a Docker image and deploy it?

If not, don’t worry, we can install it by ourselves

How does idea quickly package the SpringBoot project into a Docker image and deploy it?##4.IDEA configure docker

How does idea quickly package the SpringBoot project into a Docker image and deploy it?Note: I mentioned above 192.168.1.2 is my own server IP. Just change it to the server IP where your docker is located.

After clicking Apply, a popup will appear in your service.

Click to connect, and we will find that our docker configuration Container information

How does idea quickly package the SpringBoot project into a Docker image and deploy it?5. SpringBoot integrated Docker configuration

5.1 Install pom dependencies

Install plug-in: Docker-maven-plugin

 <!--docker-maven-plugin插件打包-->
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <!--镜像名称-->
                    <imageName>${project.artifactId}</imageName>
                    <!--指定标签-->
                    <imageTags>
                        <imageTag>latest</imageTag>
                    </imageTags>
                    <!--基础镜像jdk1.8-->
                    <baseImage>java</baseImage>
                    <!--制作者提供本人信息-->
                    <maintainer>ninesun@qq.com</maintainer>
                    <!--切换到Root目录-->
                    <workdir>/ROOT</workdir>
                    <cmd>["java", "-version"]</cmd>
                    <entryPoint>["java", "-jar", "${project.build.finalName}.jar"]</entryPoint>

                    <!--指定DockerFile路径-->
                    <!--                    <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>-->

                    <!--指定远程docker api地址-->
                    <dockerHost>http://192.168.1.2:2375</dockerHost>

                    <!-- 这里是复制 jar 包到 docker 容器指定目录配置 -->
                    <resources>
                        <resource>
                            <targetPath>/ROOT</targetPath>
                            <!--用于指定需要复制的根目录-->
                            <directory>${project.build.directory}</directory>
                            <!--用于指定需要复制的jar文件-->
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>

All my maven configurations are as follows:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.6
         
    
    com.example
    docker
    0.0.1-SNAPSHOT
    docker
    docker
    
        11
        true
        true
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
            <!--docker-maven-plugin插件打包-->
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <!--镜像名称-->
                    <imageName>${project.artifactId}</imageName>
                    <!--指定标签-->
                    <imageTags>
                        <imageTag>latest</imageTag>
                    </imageTags>
                    <!--基础镜像jdk1.8-->
                    <baseImage>java</baseImage>
                    <!--制作者提供本人信息-->
                    <maintainer>ninesun@qq.com</maintainer>
                    <!--切换到Root目录-->
                    <workdir>/ROOT</workdir>
                    <cmd>["java", "-version"]</cmd>
                    <entryPoint>["java", "-jar", "${project.build.finalName}.jar"]</entryPoint>

                    <!--指定DockerFile路径-->
                    <!--                    <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>-->

                    <!--指定远程docker api地址-->
                    <dockerHost>http://192.168.1.2:2375</dockerHost>

                    <!-- 这里是复制 jar 包到 docker 容器指定目录配置 -->
                    <resources>
                        <resource>
                            <targetPath>/ROOT</targetPath>
                            <!--用于指定需要复制的根目录-->
                            <directory>${project.build.directory}</directory>
                            <!--用于指定需要复制的jar文件-->
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
        
    

Note: After we successfully pull the plug-in, we will find that the directory structure becomes as follows:

How does idea quickly package the SpringBoot project into a Docker image and deploy it? We need to delete a startup class, otherwise the packaging will fail. I deleted the DockerApplication directly

5.2 build image

Before building the image, we need to package the project first Operation

How does idea quickly package the SpringBoot project into a Docker image and deploy it?Enter our project directory through cmd

How does idea quickly package the SpringBoot project into a Docker image and deploy it?Execute

mvn docker:build

The following information appears Indicates successful packaging

How does idea quickly package the SpringBoot project into a Docker image and deploy it? Next, return to our idea, you can see that

How does idea quickly package the SpringBoot project into a Docker image and deploy it?This is what we packaged Mirror

Execute on the server

docker images

You can also see the image information

How does idea quickly package the SpringBoot project into a Docker image and deploy it?5.3 Start the mirror

on our server Execute within

docker run -d --name idea-docker-test -p 8089:8080 docker

Note: The reason why I exposed port 8089 is because of a conflict with 8080. You can change it according to your own situation

How does idea quickly package the SpringBoot project into a Docker image and deploy it?Continue Let’s visit the test interface we wrote:

How does idea quickly package the SpringBoot project into a Docker image and deploy it?

You can see that it has been successfully deployed and can also be accessed successfully

How does idea quickly package the SpringBoot project into a Docker image and deploy it?

We can see that it can also be viewed in Idea Let’s get to the image we just started successfully and the log output

The above is the detailed content of How does idea quickly package the SpringBoot project into a Docker image and deploy it?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete