banner
biuaxia

biuaxia

"万物皆有裂痕,那是光进来的地方。"
github
bilibili
tg_channel

[Reprint] When Docker Meets Intellij IDEA, Productivity is Liberated Again~

title: 【Reprint】When Docker Meets Intellij IDEA, Productivity is Liberated Again~
date: 2021-08-06 16:51:49
comment: false
toc: true
category:

  • Java
    tags:
  • Reprint
  • Docker
  • IDEA
  • Productivity
  • Java

This article is reprinted from: When Docker Meets Intellij IDEA, Productivity is Liberated Again~ | Yudao Source Code —— Pure Source Code Analysis Blog


IDEA is a powerful tool for Java development, Spring Boot is the most popular microservice framework in the Java ecosystem, and Docker is the hottest container technology at the moment. What kind of chemical reaction will occur when they are combined together? ## Preparation Before Development

Docker installation can refer to https://docs.docker.com/install/#

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

image

Restart Docker#

systemctl daemon-reload
systemctl start docker

Open port#

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

Install IDEA plugin, restart#

image

Connect to remote Docker#

Edit configuration#

image

Fill in the remote Docker address#

image

Successful connection, remote Docker containers and images will be listed#

image

Create a New Project#

Create a Spring Boot project#

Project structure diagram

image

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>docker-demo</groupId>
    <artifactId>com.demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
        <relativePath />
    </parent>

    <properties>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
         <docker.image.prefix>com.demo</docker.image.prefix>
         <java.version>1.8</java.version>
    </properties>
    <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>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
  <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
</dependencies>
</project>

Create a docker directory under src/main and create a Dockerfile#

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

Create an application.properties file in the resource directory#

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

Create DockerApplication file#

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

Create DockerController file#

@RestController
public class DockerController {
    static Log log = LogFactory.getLog(DockerController.class);

    @RequestMapping("/")
    public String index() {
        log.info("Hello Docker!");
        return "Hello Docker!";
    }
}

Add configuration#

image

image

image

Command explanation  Image tag : Specify the image name and tag, the image name is docker-demo, and the tag is 1.1Bind ports : Bind the host port to the container internal port. The format is [host port]:[container internal port]  Bind mounts : Mount the host directory to the container internal directory. The format is [host directory]:[container internal directory]. This Spring Boot project will print logs in the container /home/developer/app/logs/ directory, and after mounting the host directory to the container internal directory, the logs will be persisted in the host directory outside the container.

Maven packaging#

image

Run#

image

image

First pull the base image, then package the image, and deploy the image to run on remote Docker

image

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

Run successful#

image

Access via browser#

image

View logs#

image

Thus, deploying a Spring Boot project to Docker via IDEA is successful! It's hard to imagine that deploying a Java web project could be so simple and convenient!

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.