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

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:亿速云. If there is any infringement, please contact admin@php.cn delete
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools