Use IDEA to connect, manage Docker and one-click deployment, and run Springboot programs

Preparation

First you need to make sure that on the target server:

  1. Installed Docker, and modify the configuration file to allow remote connection
  2. Firewall or security rules allow 2375 (non-https) 2376 (https) to install
    docker for reference: here

The IDEA version used is IntelliJ IDEA 2021.2.2 (Ultimate Edition)

Simplified Chinese language pack installed: Chinese ​(Simplified)​ Language Pack / Chinese Language Pack

The new version of IDEA has bundled and installed the docker plugin, if it is an old version, it needs to be installed manually

Press Ctrl+Alt+S to open the settings interface, select "Build, Execution, Deployment" - "Docker" to open the docker plugin management interface
insert image description here

  1. Click the + sign above the list area to add a link
  2. Fill in the name of the connection on the right (custom, easy to distinguish)
  3. We are connecting to a remote server, so choose the default "TCP socket"
  4. For non-https connection, the API URL should be filled in: tcp://server ip:2375; for https connection, https://server ip:2376 should be filled
  5. When using https connection, you need to provide the storage path of the certificate file
  6. Note: If the external network server does not use the https link, it will almost certainly be invaded into a mining machine . For the certificate generation method, refer to here

After modifying the connection configuration, the plug-in will automatically try to connect, and if successful, the following prompt will appear and
insert image description here
click OK after the configuration is successful.

Now you can see the configured connection in the "Service" of the tool button at the bottom. Double-click the connection to view the container, image, etc. in the docker.

manual deployment

In order to understand what automatic deployment does, it is necessary to understand the process of manual deployment.

total process

  1. Use the maven pacakge command to package the java program into a jar package or war package
  2. Use the docker build command to package the packaged program into a docker image
  3. Run a container using the image using the docker run command

first step

Click the IDEA sidebar tool Maven button, click the life cycle, double-click clean and then double-click the package after execution

second step

Create a new file in the project root directory (the same path as the pom.xml file), name it "Dockerfile" (no suffix name), the docker build command will look for this file in the current directory where the command is executed, and use its contents as a package operation steps.

Open the file with Notepad or other text tools and fill in the following content

# 使用openjdk:15 作为基础镜像
FROM openjdk:15
# 设置临时文件夹
VOLUME /tmp
# 将打包好的war文件复制到镜像根目录
ADD target/包名.war app.war
# 配置时区,否则打印出的日志时间会差8h
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
# 配置运行参数 这里的参数是为了解除jdk新版本的反射限制
ENV JAVA_OPTS='--add-opens java.base/java.lang=ALL-UNNAMED'
# 启动java程序命令  --spring.profiles.active=prod -> 使用 application-prod 文件作为springboot的启动配置
ENTRYPOINT java ${JAVA_OPTS} -jar /app.war --spring.profiles.active=prod

Detailed explanation:

  1. Packaging the docker image requires a base image + our program to be packaged together. The base image will provide some operating environment. Because we are a java program, we need to use a base image with jdk
  2. The sentence ADD target/package name.war app.war means adding the package name.war file in the target folder under the current directory to the image and changing the name to app.war. This file is the packaged file of maven package, which needs to be modified according to the actual file name.
  3. The last line indicates the command executed after the container starts, which is to start the java program app.war
  4. The meaning and function of the spring.profiles.active parameter are unclear, please Baidu

Therefore, we need to upload the Dockerfile and the package name.war file to a certain directory of the server, and ensure the above relative path relationship, and then execute the following command in the directory

docker build -t 镜像名称 .

in

  1. The image name is custom
  2. The final English period cannot be omitted, it means that the Dockerfile is in the current directory

Because we use the basic image, if the image does not exist locally, the server will automatically download it first, so the first run will be slower.

After successful execution, you should be able to see the image and the base image used in IDEA's "Mirror" fold, or you can use this command to view

docker images

third step

Excuting an order

docker run -d -p 宿主机端口:容器端口 --name 容器名称 镜像名称

Detailed explanation

  1. Docker can be simply understood as a virtual machine manager running on a server (host), and a container is a virtual machine
  2. -p host port: container port means mapping a port of the host to a port of the container; the host port only needs to be an open port, and the container port is the server.port set in the springboot startup configuration
  3. –name container name image name means to create and run a container using the specified image, the container name is custom, and the image name is the name of the image packaged in the second step
  4. If the program involves some file operations, because the file operations of the program in its own container will be lost when the container is recreated, it is necessary to map a certain path on the host to a certain path in the container to avoid this situation ;At this time, you need to add the parameter -v host path: the path in the container ; personally use the path of /home/project name on both sides

Then execute the following command to view the running container

docker ps

You should be able to see that the container with the specified name is running. At this time, you should be able to access the program interface by using http://server ip:host port

Tips

Execute the ifconfig command on the host machine, and you can see a network named docker0. Its inet ip should be in the form of: 172.17.0.1. For each running container, use this ip + the above-mentioned "container port" , you can access external services of other containers, for example:

  1. Backend programs can access databases and redis in other containers
  2. Front-end programs can access back-end interfaces in other containers

This ip is a fixed intranet ip, which is not affected by the deployed server ip, nor is it affected by the restart of the docker service (the ip of each container will change).

One-click deployment

Modify the pom file

We need to add a docker-maven plugin to perform steps 1-2 in manual deployment with one click, but we need to keep the previous Dockerfile .

Open the pom.xml file, find the plugins tag in the build tag, and add the following content to it (note that if the plugin has the same name, please delete it yourself)

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <excludes>
            <exclude>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
            </exclude>
        </excludes>
    </configuration>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <!--跳过测试-->
        <skip>true</skip>
    </configuration>
</plugin>

<!--docker-maven插件-->
<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>1.2.2</version>
    <executions>
        <execution>
            <id>build-image</id>
            <phase>package</phase>
            <!--表示在 package完成后执行 docker  build 命令-->
            <goals>
                <goal>build</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <!--Docker 的主机地址-->
        <!-- http访问时 端口为2375  https访问时 端口为2376-->
        <dockerHost>https://服务器ip:2376</dockerHost>
        <!--docker build 生成的镜像的名称 注意不要有大写-->
        <imageName>镜像名称</imageName>
        <!-- 使用https 证书访问时需要提供证书文件的存放路径-->
        <dockerCertPath>证书文件的存放路径</dockerCertPath>
        <!--镜像的 tags-->
        <forceTags>true</forceTags>
        <!--Dockerfile 的位置 表示在项目根目录-->
        <dockerDirectory>${project.basedir}</dockerDirectory>
        <resources>
            <resource>
                <targetPath>/</targetPath>
                <directory>${project.build.directory}</directory>
                <include>${project.build.finalName}.war</include>
            </resource>
        </resources>
    </configuration>
</plugin>

Pay attention to modify the server ip, image name, and certificate file storage path; the server ip and certificate path are exactly the same as the configuration when linking to docker, and the meaning of the image name is consistent with the image name in the docker build step.

Pay attention to reloading after modification to make the configuration take effect.
insert image description here
When the first step of manual deployment is performed after the configuration is completed, the second step will also be executed together (execute until the docker image is packaged)

layout script

Next, we make the original first step and third part into scripts to execute with one click

Click on the drop down and select "Edit Configuration"
insert image description here

Pack

Click the upper left + sign to select Maven
insert image description here

  1. Customize the name. It is recommended to check "Save as project file", so that this script will be saved in the project as a file, that is, it can be saved on github.
  2. Write clean package on the command line , that is, execute the clean and package instructions in sequence (the original first step)
  3. After confirming the completion, select this script in the drop-down box, and click the right arrow to execute to complete the packaging
  4. Note: If you repackage after modifying the code, the new image will use the name we set, and the old image will appear in the form of image id; it needs to be deleted in time, if the previous container is still running, you need to delete the container first .

insert image description here

If the package is successful, you should see something like this in the console

[INFO] Built 镜像名称
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  01:33 min
[INFO] Finished at: 2021-09-18T15:01:55+08:00
[INFO] ------------------------------------------------------------------------

run

Click the upper left + sign to select the Docker image
insert image description here

  1. name customization
  2. The server selects the previously configured docker connection
  3. The image ID or name is the aforementioned image name; the container name is the aforementioned container name
  4. The binding port is the aforementioned -p host port: container port parameter
  5. Bind mount is the aforementioned -v host path: path parameter in the container
  6. The --restart always in the running option means that the container runs with the start of the docker service, and if it crashes and exits during the running process, it will automatically restart.
  7. After confirming the completion, select this script to run after the packaging is completed, and the container can be started. If a container with the same name already exists, it will be deleted first.

insert image description here

redeployment operation

After the above configuration is completed, each time the code is modified, the two scripts are executed once to complete the redeployment.

Guess you like

Origin blog.csdn.net/hjg719/article/details/120364279
Recommended