Detailed process of SpringBoot integrating Docker

Detailed process of SpringBoot integrating Docker

I have added a lot of new things to my memo recently, but I am not used to the new environment and have no motivation to organize my notes.

1. Demo Project

First, prepare a simple project to deploy to the Docker host and verify whether the project runs successfully.

1.1 Interface Preparation

Prepare a test interface to verify whether the Docker deployment is successful

@RestController
@RequestMapping("/")
public class HelloController {

    @GetMapping("/hello")
    public String hide() {
        return "Hello World";
    }
}

1.2 Configuration Preparation

Add the Docker host address in the application.yml file to facilitate unified management (this feature requires the installation of dependent plug-ins)

server:
  port: 8080

spring:
  application:
    name: docker-deploy-test

docker:
  host: http://xxx.xxx.xxx.xxx:2375

2. Docker opens remote connection

Docker is also a Client/Serve architecture (dameon daemon), which can only be accessed locally by default, so we have to set it to be accessible from the Internet before we can deploy the project to Docker.

1.1 Modify the configuration file

Edit the configuration on the Docker host and add the following line of configuration (which means that any address can access port 2375)

$vim /usr/lib/systemd/system/docker.service
 
# ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H fd:// --containerd=/run/containerd/containerd.sock

1.2 Refresh configuration and restart

$ systemctl daemon-reload
$ systemctl restart docker

1.3 Authentication Login

Directly open port 2357, so anyone can push things to the Docker host. I was pushed a mining image at the beginning, and the host was stuck and I couldn’t log in. Later, I had no choice but to reinstall the system image. Docker supports logging in using SSL certificates. If you are in an external network environment, be sure to use SSL to log in (the author has recorded OpenSSL notes ---- for authentication, but has not had time to organize them)

3. Install the Docker plugin in IDEA

The 2019 version of IDEA I used has the Docker plug-in integrated by default and does not need to be installed again. Just match the remote host address and port for IDEA to connect to, and finally verify whether it is connected to the remote Docker host.

Create a new Docker here, fill in the host address and it will automatically connect. If it shows Connection successful, it means that IDEA is connected to the host.

4. Add Docker plugin to Maven

The IDEA plug-in in the previous step can build and deploy images, and can also manage Docker. But the author uses Maven plugin and IDEA plugin together, which can facilitate program construction and deployment

<!-- docker plugin -->
<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>1.0.0</version>

    <!-- Bind docker:build to maven package lifecycle -->
    <executions>
        <execution>
            <id>docker-build-image</id>
            <phase>package</phase>
            <goals>
                <goal>build</goal>
            </goals>
        </execution>
    </executions>

    <!-- Plugin Configuration -->
    <configuration>
        <!-- Name of the built image: tag-->
        <imageName>${project.artifactId}:${project.version}</imageName>
        <!-- Path to store dockerfile-->
        <dockerDirectory>${project.basedir}</dockerDirectory>
        <!-- Docker Host address, parsed from application.yml by the plugin during the initialize phase -->
        <dockerHost>${docker.host}</dockerHost>
        <!-- SSL authentication certificate address, if SSL login is enabled-->
        <!-- <dockerCertPath></dockerCertPath> -->
        <resources>
            <resource>
                <!-- What resources are used to build the image (that is, the jar package), and the directory where the image is pushed to the container-->
                <targetPath>/</targetPath>
                <directory>${project.build.directory}</directory>
                <include>${project.build.finalName}.jar</include>
            </resource>
        </resources>
    </configuration>
</plugin>

5. Write Dockerfile

Create a Dockerfile file, fill in the following content, and then put it in the root directory of the project

FROM openjdk:8-jdk-alpine

MAINTAINER [author] [[email protected]]

ARG JAR_FILE=/target/*.jar

COPY ${JAR_FILE} app.jar

EXPOSE 8080

ENTRYPOINT ["java","-jar","/app.jar"] 

6. Package the project

The Maven plugin binds the execution process of Package and Docker:build, that is, after the project is packaged into a jar, Docker:build will be automatically executed to build the image and publish it to the remote Docker host (if the project is large, it will take a long time to wait for the image to be pushed to the Docker host)

7. Create a container

You can manage Docker in the IDEA plug-in. Click on the services in the lower right corner and you can see the image just built in the connected Docker. Then you can right-click this image to create a container.

In the pop-up box, enter the name of the container to be created and the port mapped to the container, then click Run and wait for the container to start. After the startup is complete, you can see the newly created container in the IDEA plug-in. Click it to view the details of the container.

8. Verify deployment

Use the host's IP address or domain name to access the test interface just released

This is the end of this article about SpringBoot integration with Docker. For more relevant SpringBoot integration with Docker content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Implementation steps for Docker deployment of SpringBoot applications
  • Detailed process of integrating docker with idea to quickly deploy springboot applications
  • Introduction to Docker Quick Deployment of SpringBoot Project

<<:  How to use dl(dt,dd), ul(li), ol(li) in HTML

>>:  Table of CSS Bugs Caused by hasLayout

Recommend

An article to quickly understand Angular and Ionic life cycle and hook functions

Table of contents Angular accomplish Calling orde...

v-html rendering component problem

Since I have parsed HTML before, I want to use Vu...

Detailed explanation of Linux command file overwrite and file append

1. The difference between the command > and &g...

A detailed introduction to the Linux directory structure

When you first start learning Linux, you first ne...

8 examples of using killall command to terminate processes in Linux

The Linux command line provides many commands to ...

MySQL 5.7.15 version installation and configuration method graphic tutorial

This article shares with you a detailed tutorial ...

Detailed explanation of HTML style tags and related CSS references

HTML style tag style tag - Use this tag when decl...

How to install docker using YUM

As shown in the following figure: If the version ...

How to find identical files in Linux

As the computer is used, a lot of garbage will be...

Detailed steps for installing Tomcat, MySQL and Redis with Docker

Table of contents Install Tomcat with Docker Use ...

Understanding MySQL Locking Based on Update SQL Statements

Preface MySQL database lock is an important means...