Table of Contents
1. Modify the docker configuration file
5.1 Install pom dependencies
Install plug-in: Docker-maven-plugin
Before building the image, we need to package the project first Operation
on our server Execute within
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?

May 21, 2023 pm 09:28 PM
docker idea springboot

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:

1

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

Copy after login

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

1

touch daemon.json

Copy after login

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:

1

2

3

4

5

6

#重新加载配置文件                 

systemctl daemon-reload

# 重启服务

systemctl restart docker.service

# 查看端口是否开启 默认端口2375

netstat -anp|grep 2375

Copy after login

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

1

2

3

4

5

6

添加指定需要开放的端口:

firewall-cmd --zone=public --add-port=2375/tcp --permanent

重载入添加的端口:

firewall-cmd --reload

查询指定端口是否开启成功:

firewall-cmd --query-port=2375/tcp

Copy after login

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

1

curl http://127.0.0.1:2375/info

Copy after login

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

<!--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>

Copy after login

All my maven configurations are as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

<!--?xml version="1.0" encoding="UTF-8"?-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelversion>4.0.0</modelversion>

    <parent>

        <groupid>org.springframework.boot</groupid>

        <artifactid>spring-boot-starter-parent</artifactid>

        <version>2.6.6</version>

        <relativepath> <!-- lookup parent from repository -->

    </relativepath></parent>

    <groupid>com.example</groupid>

    <artifactid>docker</artifactid>

    <version>0.0.1-SNAPSHOT</version>

    <name>docker</name>

    <description>docker</description>

    <properties>

        <java.version>11</java.version>

        <maven.test.skip>true</maven.test.skip>

        <maven.javadoc.skip>true</maven.javadoc.skip>

    </properties>

    <dependencies>

        <dependency>

            <groupid>org.springframework.boot</groupid>

            <artifactid>spring-boot-starter-web</artifactid>

        </dependency>

 

        <dependency>

            <groupid>org.springframework.boot</groupid>

            <artifactid>spring-boot-starter-test</artifactid>

            <scope>test</scope>

        </dependency>

    </dependencies>

 

    <build>

        <plugins>

            <plugin>

                <groupid>org.springframework.boot</groupid>

                <artifactid>spring-boot-maven-plugin</artifactid>

            </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>

        </plugins>

    </build>

 

</project>

Copy after login

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

1

mvn docker:build

Copy after login

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

1

docker images

Copy after login

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

1

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

Copy after login

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to exit the container by docker How to exit the container by docker Apr 15, 2025 pm 12:15 PM

Four ways to exit Docker container: Use Ctrl D in the container terminal Enter exit command in the container terminal Use docker stop &lt;container_name&gt; Command Use docker kill &lt;container_name&gt; command in the host terminal (force exit)

How to update the image of docker How to update the image of docker Apr 15, 2025 pm 12:03 PM

The steps to update a Docker image are as follows: Pull the latest image tag New image Delete the old image for a specific tag (optional) Restart the container (if needed)

How to copy files in docker to outside How to copy files in docker to outside Apr 15, 2025 pm 12:12 PM

Methods for copying files to external hosts in Docker: Use the docker cp command: Execute docker cp [Options] &lt;Container Path&gt; &lt;Host Path&gt;. Using data volumes: Create a directory on the host, and use the -v parameter to mount the directory into the container when creating the container to achieve bidirectional file synchronization.

How to restart docker How to restart docker Apr 15, 2025 pm 12:06 PM

How to restart the Docker container: get the container ID (docker ps); stop the container (docker stop &lt;container_id&gt;); start the container (docker start &lt;container_id&gt;); verify that the restart is successful (docker ps). Other methods: Docker Compose (docker-compose restart) or Docker API (see Docker documentation).

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to view the docker process How to view the docker process Apr 15, 2025 am 11:48 AM

Docker process viewing method: 1. Docker CLI command: docker ps; 2. Systemd CLI command: systemctl status docker; 3. Docker Compose CLI command: docker-compose ps; 4. Process Explorer (Windows); 5. /proc directory (Linux).

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

How to use docker desktop How to use docker desktop Apr 15, 2025 am 11:45 AM

How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

See all articles