Docker meets Intellij IDEA, Java development improves productivity tenfold

Docker meets Intellij IDEA, Java development improves productivity tenfold

Idea is a powerful tool for Java development, SpringBoot is the most popular microservice framework in the Java ecosystem, and Docker is the hottest container technology nowadays. So what kind of chemical reaction will occur when they are combined together?

1. Preparation before development

1. For Docker installation, please refer to https://docs.docker.com/install/

2. Configure Docker remote connection port

 vi /usr/lib/systemd/system/docker.service

Find ExecStart and add -H tcp://0.0.0.0:2375 at the end, as shown in the figure below

3. Restart Docker

systemctl daemon-reload
 systemctl restart docker

4. Open ports

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

5.Idea installs the docker plugin and restarts

6. Connect to remote docker

(1) Edit configuration

(2) Fill in the remote docker address

(3) If the connection is successful, the remote Docker container and image will be listed.

2. New Project

Create a springboot project

Project structure diagram

(1) Configure the pom file

<?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>com.fengqi</groupId>
    <artifactId>dockerDemo</artifactId>
    <version>1.0.0</version>
    <relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.fengqi</groupId>
  <artifactId>web</artifactId>
  <version>1.0.0</version>
  <name>web</name>
  <description>Demo project for Spring Boot</description>
  
 <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>
 
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>
  </dependencies>
 
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
      <plugin>
        <groupId>com.spotify</groupId>
        <artifactId>docker-maven-plugin</artifactId>
        <version>1.0.0</version>
        <configuration>
          <dockerDirectory>src/main/docker</dockerDirectory>
          <resources>
            <resource>
              <targetPath>/</targetPath>
              <directory>${project.build.directory}</directory>
              <include>${project.build.finalName}.jar</include>
            </resource>
          </resources>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <executions>
          <execution>
            <phase>package</phase>
            <configuration>
              <tasks>
                <copy todir="src/main/docker" file="target/${project.artifactId}-${project.version}.${project.packaging}"></copy>
              </tasks>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

(2) Create a docker directory in the src/main directory and create a Dockerfile file

FROM openjdk:8-jdk-alpine
ADD *.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

(3) Create an application.properties file in the resource directory

logging.config=classpath:logback.xml
logging.path=/home/developer/app/logs/
server.port=8990

(4) Create a DockerApplication file

@SpringBootApplication
public class DockerApplication {
 public static void main(String[] args) {
 SpringApplication.run(DockerApplication.class, args);
 }
}

(5) Create a DockerController file

@RestController
public class DockerController {
 static Log log = LogFactory.getLog(DockerController.class);
 @RequestMapping("/")
 public String index() {
 log.info("Hello Docker!");
 return "Hello Docker!";
 }
}

(6) Add configuration

Command Explanation

Image tag: Specify the image name and tag. The image name is docker-demo and the tag is 1.1.

Bind ports: Bind host ports to container internal ports. The format is [host port]:[container internal port]

Bind mounts: Mount the host directory to the container's internal directory. The format is [host directory]:[container internal directory]

This springboot project will print logs in the container /home/developer/app/logs/ directory. After mounting the host directory to the container's internal directory, the logs will be persisted in the host directory outside the container.

(7) Maven packaging

(8) Run

Here we can see that the image name is docker-demo:1.1 and the docker container is docker-server

(9) Successful operation

(10) Browser access

(11) Log View

Since then, the springboot project has been deployed to docker successfully through idea! It's hard to imagine that deploying a Java web project is so simple and convenient!

Finally, I would like to share with you the relevant learning tutorials:

https://www.bilibili.com/video/BV14t411z77T

IDEA Tutorial

https://www.bilibili.com/video/BV1PZ4y1j7QK

This is the end of this article about Docker meets Intellij IDEA, which improves Java development productivity tenfold. For more related content about Docker meets IDEA, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed steps to deploy SpringBoot projects using Docker in Idea
  • Detailed tutorial on how to publish springboot projects through docker plug-in in IDEA
  • A brief analysis of SpringBoot packaging and uploading to docker and implementing multi-instance deployment (IDEA version)
  • Deploy the springboot project to docker based on idea
  • Java remote one-click deployment of springboot to Docker through Idea

<<:  Mysql delete data and data table method example

>>:  js to achieve the effect of dragging the slider

Recommend

Vue uses mixins to optimize components

Table of contents Mixins implementation Hook func...

Should I abandon JQuery?

Table of contents Preface What to use if not jQue...

Detailed steps for using jib for docker deployment in Spring Cloud

Introduction to Jib Jib is a library developed by...

CenOS6.7 mysql 8.0.22 installation and configuration method graphic tutorial

CenOS6.7 installs MySQL8.0.22 (recommended collec...

Docker connects to the host Mysql operation

Today, the company project needs to configure doc...

MySQL 8.0.12 decompression version installation tutorial personal test!

Mysql8.0.12 decompression version installation me...

Independent implementation of nginx container configuration file

Create a container [root@server1 ~]# docker run -...

How to query duplicate data in mysql table

INSERT INTO hk_test(username, passwd) VALUES (...

In-depth analysis of the diff algorithm in React

Understanding of diff algorithm in React diff alg...

Use iptables and firewalld tools to manage Linux firewall connection rules

Firewall A firewall is a set of rules. When a pac...

Typora code block color matching and title serial number implementation code

Effect: The title has its own serial number, the ...

How to use JSX to implement Carousel components (front-end componentization)

Before we use JSX to build a component system, le...