Docker + Intellij IDEA,提升 10 倍生产力!
好好学java
共 3300字,需浏览 7分钟
·
2022-01-02 23:30
今日推荐
Idea是Java开发利器,springboot是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」,如下图所示
![](https://filescdn.proginn.com/cb8e4a96ee8fcd9b5cc94387a7045696/2849764e5ab6fc5f50230bd2b6b4eeda.webp)
3. 重启docker
systemctl daemon-reload
systemctl start docker
复制代码
4. 开放端口
firewall-cmd --zone=public --add-port=2375/tcp --permanent
5. Idea安装插件,重启
![](https://filescdn.proginn.com/431ab565b7dbeb00d403fd8d5923c87d/5566cf8fbcca0de47d77ae9c1fed9266.webp)
6. 连接远程docker
(1) 编辑配置
![](https://filescdn.proginn.com/261e1c9e1180cdc76546640004003828/d1a4ae50da2353010d779e6666ff79ca.webp)
(2) 填远程docker地址
![](https://filescdn.proginn.com/308465fbfbf747057d223e78f1d4ae84/c429a93ad21521feb8ae85821651bf87.webp)
(3) 连接成功,会列出远程docker容器和镜像
![](https://filescdn.proginn.com/3c6c68cd657c31ade26c2a11b068efc4/0c477c906c9cfb30fba3593424e76505.webp)
二、新建项目
1. 创建springboot项目
项目结构图
![](https://filescdn.proginn.com/74db6154b87271d9fa83f0eb074423a4/c60ffc21ff438be5dae64b4c87b02349.webp)
(1) 配置pom文件
<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.0modelVersion>
<groupId>docker-demogroupId>
<artifactId>com.demoartifactId>
<version>1.0-SNAPSHOTversion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.2.RELEASEversion>
<relativePath />
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<docker.image.prefix>com.demodocker.image.prefix>
<java.version>1.8java.version>
properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
<plugin>
<groupId>com.spotifygroupId>
<artifactId>docker-maven-pluginartifactId>
<version>1.0.0version>
<configuration>
<dockerDirectory>src/main/dockerdockerDirectory>
<resources>
<resource>
<targetPath>/targetPath>
<directory>${project.build.directory}directory>
<include>${project.build.finalName}.jarinclude>
resource>
resources>
configuration>
plugin>
<plugin>
<artifactId>maven-antrun-pluginartifactId>
<executions>
<execution>
<phase>packagephase>
<configuration>
<tasks>
<copy todir="src/main/docker" file="target/${project.artifactId}-${project.version}.${project.packaging}">copy>
tasks>
configuration>
<goals>
<goal>rungoal>
goals>
execution>
executions>
plugin>
plugins>
build>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
dependencies>
project>
(2) 在src/main目录下创建docker目录,并创建Dockerfile文件
FROM openjdk:8-jdk-alpine
ADD *.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
(3) 在resource目录下创建application.properties文件
logging.config=classpath:logback.xml
logging.path=/home/developer/app/logs/
server.port=8990
(4) 创建DockerApplication文件
@SpringBootApplication
public class DockerApplication {
public static void main(String[] args) {
SpringApplication.run(DockerApplication.class, args);
}
}
(5) 创建DockerController文件
@RestController
public class DockerController {
static Log log = LogFactory.getLog(DockerController.class);
@RequestMapping("/")
public String index() {
log.info("Hello Docker!");
return "Hello Docker!";
}
}
(6) 增加配置
命令解释
「Image tag :」 指定镜像名称和「tag」,镜像名称为 「docker-demo」,「tag」为「1.1」 「Bind ports :」 绑定宿主机端口到容器内部端口。格式为[宿主机端口]:[容器内部端口] 「Bind mounts :」 将宿主机目录挂到到容器内部目录中。格式为[宿主机目录]:[容器内部目录]。这个springboot项目会将日志打印在容器 「/home/developer/app/logs/」 目录下,将宿主机目录挂载到容器内部目录后,那么日志就会持久化容器外部的宿主机目录中。
(7) Maven打包
![](https://filescdn.proginn.com/c19d5ac21a49e8b6fdc091d67fe4c769/8993710beda2ae6f048c6231d5ad45f6.webp)
(8) 运行
先pull基础镜像,然后再打包镜像,并将镜像部署到远程docker运行
这里我们可以看到镜像名称为docker-demo:1.1,docker容器为docker-server
(9) 运行成功
![](https://filescdn.proginn.com/bd8e3a8874e7bb1007655d0842fef632/825cb46fdc523cff72baf450adc3978c.webp)
(10) 浏览器访问
![](https://filescdn.proginn.com/e86bbc001cff2abbf7b27bbf4f2b1bf1/b06549a0db9d19a2c4d35320152d52c5.webp)
(11) 日志查看
![](https://filescdn.proginn.com/326333e353c94f43d40c1219ad9638ce/11d1674948561a6ab60635e06fafcf3d.webp)
自此通过idea 部署springboot项目到docker成功!难以想象,部署一个Javaweb项目竟然如此简单方便!
作者:CodingTao
链接:https://juejin.cn/post/6844903865192562696
推荐文章
评论