Detailed tutorial on how to connect to a remote server Docker to deploy a Spring Boot project in IDEA

Detailed tutorial on how to connect to a remote server Docker to deploy a Spring Boot project in IDEA

Before you begin

Have a cloud server, mine is Tencent Cloud Server (CentOS7)

Tencent Cloud Server installs Docker. My version is Docker 19.03.9. For details on installing and configuring the image accelerator, please refer to my other blog post: https://www.jb51.net/article/188048.htm, which has a detailed explanation

IDEA is installed on Windows

Officially started work

Step 1: Configure Docker and enable remote access (Note: This is for learning reference only. It is absolutely not allowed in actual production environments, as it will create great security risks). The default port is 2375, which can also be changed to other ports.

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

vim /lib/systemd/system/docker.service

Append -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock after ExecStart

After modification, remember to write the save and exit command: wq

2. Reload the configuration file and restart Docker to make the configuration take effect

#Reload the configuration file systemctl daemon-reload
#Restart the docker service systemctl restart docker

3. Use the curl command to check whether it is enabled

#curl download docker service information curl http://127.0.0.1:2375/info

If there is a return statement, it proves that the startup is successful, and you can see my Docker version, image accelerator address and other information, but at this time the remote access may still be unavailable

4. Configure Tencent Cloud security group rules and open port 2375 in the inbound rules to enable remote access (the same as Alibaba Cloud)

Finally, click Finish and it’s OK

5. Check whether the port can be accessed remotely. Enter in the local browser: http://server IP address:2375/info

Step 2: Configure the project and connect to the remote docker

1. Install the Docker plug-in. Go to Plugins and search for Docker, then install it directly. If I have already installed it, restart IDEA after the installation is complete, and the docker plug-in will be available.

2. Configure the connection to the remote docker, open the Docker plug-in, create a new connection, enter the server IP address: 2375 in the Engine API URL, and then, if nothing unexpected happens, Connection successful will appear below, so that the Docker connection to the remote server is successful

3. Edit the project pom.xml file and add docker-maven-plugin plug-in to the Spring Boot project

<plugin>
 <groupId>com.spotify</groupId>
 <artifactId>docker-maven-plugin</artifactId>
 <version>1.0.0</version>
 <!--Bind the plugin to a certain phase for execution-->
 <executions>
 <execution>
 <id>build-image</id>
 <!--Bind the plug-in to the package phase. That is to say, users only need to execute mvn package, which will automatically execute mvn docker:build-->
 <phase>package</phase>
 <goals>
 <goal>build</goal>
 </goals>
 </execution>
 </executions>
 <configuration>
 <!--Specify the generated image name-->
 <imageName>wbb/${project.artifactId}</imageName>
 <!--Specify tags-->
 <imageTags>
 <imageTag>latest</imageTag>
 </imageTags>
 <!-- Specify the Dockerfile path ${project.basedir}: under the project root path -->
 <dockerDirectory>${project.basedir}</dockerDirectory>
 <!--Specify the remote docker api address-->
 <dockerHost>http://134.175.236.221:2375</dockerHost>
 <!-- Here is the configuration for copying the jar package to the specified directory of the docker container-->
 <resources>
 <resource>
 <targetPath>/</targetPath>
 <!--The path where the jar package is located corresponds to the target directory -->
 <directory>${project.build.directory}</directory>
 <!-- The jar package that needs to be included, which corresponds to the file name added in Dockerfile-->
 <include>${project.build.finalName}.jar</include>
 </resource>
 </resources>
 <!-- The following two lines are used for docker push to DockerHub. -->
 <!--<serverId>docker-hub</serverId>-->
 <!--<registryUrl>https://index.docker.io/v1</registryUrl>-->
 </configuration>
 </plugin>

4. Create a Dockerfile file in the project root directory

Dockerfile content

For the analysis of Dockerfile reserved words, please refer to my other blog post: https://www.jb51.net/article/102902.htm, which contains detailed analysis of commonly used reserved words, but there is no example. You can use this Dockerfile file for comparison and understanding

The contents of this Dockerfile are roughly as follows:

The first line: The image to be created is based on the java:8 image, which is JDK8, which means that the basic Java runtime development environment must be available. This can also be docker pull in advance

The second line: When building, add the jar package in the target directory to the image and rename it to app.jar

The third line: The port that the image opens to the outside world is 8888. This will be used later when mapping ports. Be sure to remember it. Of course, if you don’t remember it, you can also use the docker history command to view it.

Line 4: When the image instance container is started, the java -jar /app.jar command will be automatically executed. That is, when the container is started, the project is started and will not be overwritten by the command parameters added when the container is started.

Other parameters that are not specified are default. For example, the default path when logging into the container is the root directory.

5. Package the project and automatically build the image. Click Maven Projects on the right and double-click package

If you see the information, it means the build is successful. Now you can go to the server to view the built image.

Step 3: Start the image instance container and access the project remotely. The following steps can be performed directly in IDEA or on the server. In order to become more familiar with the docker command, I demonstrated it on the server side.

For the docker commands used below, you can check out my other two blog posts: https://www.jb51.net/article/113227.htm https://www.jb51.net/article/142481.htm, which contain detailed explanations of docker image commands and container commands.

1. View all images: docker images

As shown in the figure, the image wbb/shop image just built already exists

2. Start the container: docker run -it -p 8889:8888 --name shop wbb/shop

When starting, -p 8889:8888 here is port mapping, that is, port 8889 of the host is mapped to port 8888 of the image. Accessing port 8889 of the host is equivalent to accessing port 8888 of the image. This 8888 is the open port written in the Dockerfile file. Of course, it should generally be specified to run in the background with -d. I specify foreground interactive operation. In fact, it is just to see the beauty in the painting. This figure is the goddess of dreams.

3. View the container running status information: docker ps

As you can see, the built image instance has been running for more than 7 minutes. The port mapping is as follows:

4. Remote access project: http://server IP address:8889/shop

Before that, you must ensure that port 8889 of the host is open. Otherwise, you can release port 8889 according to the configuration security group rules. In addition, /shop is the virtual path of my project.

OK, here we have explained how to use IDEA to connect to the remote Docker service to deploy the Spring Boot project. Please give us your advice if there are any shortcomings.

Appendix: Check whether the port is open for remote access. You can also use the online port scanning tool: http://www.jsons.cn/port/, enter the server IP address and the corresponding port scan, and you can scan multiple ports, which is very convenient.

Summarize

This is the end of this article about IDEA connecting to a remote server Docker to deploy a Spring Boot project. For more related idea docker deployment springboot project content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Steps for IDEA to integrate Docker to achieve remote deployment
  • Detailed steps for IDEA to integrate docker to achieve remote deployment
  • Idea deploys remote Docker and configures the file
  • How to use Docker plugin to remotely deploy projects to cloud servers in IDEA
  • Java remote one-click deployment of springboot to Docker through Idea
  • Implementation of IDEA remote management of docker images and container services

<<:  MySQL full backup and quick recovery methods

>>:  Vue's vue.$set() method source code case detailed explanation

Recommend

Native js to achieve puzzle effect

This article shares the specific code of native j...

MySQL 8.0.12 winx64 decompression version installation graphic tutorial

Recorded the installation of mysql-8.0.12-winx64 ...

Design a data collector with vue

Table of contents Scenario Core Issues Status mon...

Detailed explanation of Vue's sync modifier

Table of contents 1. Instructions 2. Modifiers 3....

Teach you MySQL query optimization analysis tutorial step by step

Preface MySQL is a relational database with stron...

Windows Server 2008 Tutorial on Monitoring Server Performance

Next, we will learn how to monitor server perform...

CSS3 animation – steps function explained

When I was looking at some CSS3 animation source ...

Detailed explanation of the basic usage of VUE watch listener

Table of contents 1. The following code is a simp...

Nodejs combined with Socket.IO to realize websocket instant communication

Table of contents Why use websocket Socket.io Ope...

Summary of common problems and solutions in Vue (recommended)

There are some issues that are not limited to Vue...

Comprehensive understanding of HTML Form elements

As shown below: XML/HTML CodeCopy content to clip...

Image hover toggle button implemented with CSS3

Result:Implementation Code html <ul class=&quo...

HTML table tag tutorial (33): cell vertical alignment attribute VALIGN

In the vertical direction, you can set the cell a...

Native JS encapsulation vue Tab switching effect

This article example shares the specific code of ...