当Docker遇到Intellij IDEA,再次解放了生产力


IDEA是Java开发利器,Spring Boot是Java生态中最流行的微服务框架,Docker是时下最火的容器技术,那么它们结合在一起会产生什么化学反应呢?

开发前准备

1、Docker的安装可以参考:https://docs.docker.com/install/

2、配置Docker远程连接端口:
vi /usr/lib/systemd/system/docker.service

找到ExecStart,在最后面添加-H tcp://0.0.0.0:2375,如下图所示:
1.png

3、重启Docker
systemctl daemon-reload
systemctl start docker

4、开放端口
firewall-cmd --zone=public --add-port=2375/tcp --permanent  

5、IDEA安装插件,重启:
2.png

6、连接远程Docker
  1. 编辑配置:
    3.png
  2. 填远程Docker地址:
    4.png
  3. 连接成功,会列出远程Docker容器和镜像:
    5.png


新建项目

创建Spring Boot项目

项目结构图:
6.png

配置pom文件:
<?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>

在src/main目录下创建Docker目录,并创建Dockerfile文件

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

在resource目录下创建application.properties文件

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

创建DockerApplication文件

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


创建DockerController文件

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

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


增加配置

7.png


8.png


9.png

命令解释:
  • Image tag:指定镜像名称和tag,镜像名称为docker-demo,tag为1.1
  • Bind ports:绑定宿主机端口到容器内部端口。格式为[宿主机端口]:[容器内部端口]
  • Bind mounts:将宿主机目录挂到到容器内部目录中。格式为[宿主机目录]:[容器内部目录]。这个Spring Boot项目会将日志打印在容器/home/developer/app/logs/目录下,将宿主机目录挂载到容器内部目录后,那么日志就会持久化容器外部的宿主机目录中。


Maven打包

10.png

运行

11.png

12.png

先pull基础镜像,然后再打包镜像,并将镜像部署到远程Docker运行。
13.png

这里我们可以看到镜像名称为docker-demo:1.1,Docker容器为docker-server。

运行成功

14.png

浏览器访问

15.png

日志查看

16.png

自此通过IDEA部署Spring Boot项目到Docker成功!难以想象,部署一个Javaweb项目竟然如此简单方便!

原文链接:https://juejin.im/post/6844903865192562696,作者:陶章好

0 个评论

要回复文章请先登录注册