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

A brief discussion on HTML doctype and encoding

DOCTYPE Doctype is used to tell the browser which...

What you need to know about responsive design

Responsive design is to perform corresponding ope...

WeChat applet example of using functions directly in {{ }}

Preface In WeChat applet development (native wxml...

Monitor changes in MySQL table content and enable MySQL binlog

Preface binlog is a binary log file, which record...

Common writing examples for MySQL and Oracle batch insert SQL

Table of contents For example: General writing: S...

Resolving MySQL implicit conversion issues

1. Problem Description root@mysqldb 22:12: [xucl]...

MySQL installation tutorial under Centos7

MySQL installation tutorial, for your reference, ...

CSS3 filter code to achieve gray or black mode on web pages

front end css3,filter can not only achieve the gr...

In-depth explanation of InnoDB locks in MySQL technology

Table of contents Preface 1. What is a lock? 2. L...

How to Develop a Progressive Web App (PWA)

Table of contents Overview Require URL of the app...

JS implements a simple todoList (notepad) effect

The notepad program is implemented using the thre...

MySQL users and permissions and examples of how to crack the root password

MySQL Users and Privileges In MySQL, there is a d...