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

Analysis and practice of React server-side rendering principle

Most people have heard of the concept of server-s...

IIS7~IIS8.5 delete or modify the server protocol header Server

Requirements: Remove HTTP response headers in IIS...

How to change the color of the entire row (tr) when the mouse stops in HTML

Use pure CSS to change the background color of a ...

CSS beginner tutorial: background image fills the entire screen

If you want the entire interface to have a backgr...

How to implement controllable dotted line with CSS

Preface Using css to generate dotted lines is a p...

mysql obtains statistical data within a specified time period

mysql obtains statistical data within a specified...

Example of using Nginx to implement port forwarding TCP proxy

Table of contents Demand Background Why use Nginx...

Pure HTML+CSS to achieve typing effect

This article mainly introduces the typing effect ...

The most basic code for web pages

◆Add to favorites illustrate Click to add your we...

Practice of Vue global custom instruction Modal drag

Table of contents background Implementation ideas...

Django+mysql configuration and simple operation database example code

Step 1: Download the mysql driver cmd enters the ...

Vue.js $refs usage case explanation

Despite props and events, sometimes you still nee...

Do you know what are the ways to jump routes in Vue?

Table of contents The first method: router-link (...

A brief discussion on the definition and precautions of H tags

Judging from the results, there is no fixed patte...