这篇文章主要介绍了Docker遇到Intellij IDEA,Java开发提升了十倍生产力,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
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,如下图所示
![图片[1]-Docker遇到Intellij IDEA,Java开发提升了十倍生产力_docker-安全小天地](https://img.godyu.com/2023/12/20231226094526234.jpg?imageView2/0/format/webp/q/75)
3. 重启docker
systemctl daemon-reload systemctl restart docker
4.开放端口
firewall-cmd --zone=public --add-port=2375/tcp --permanent
5.Idea安装docker插件,重启
![图片[2]-Docker遇到Intellij IDEA,Java开发提升了十倍生产力_docker-安全小天地](https://img.godyu.com/2023/12/20231226174527488.jpg?imageView2/0/format/webp/q/75)
6.连接远程docker
(1) 编辑配置
![图片[3]-Docker遇到Intellij IDEA,Java开发提升了十倍生产力_docker-安全小天地](https://img.godyu.com/2023/12/20231226174528543.jpg?imageView2/0/format/webp/q/75)
(2) 填远程docker地址
![图片[4]-Docker遇到Intellij IDEA,Java开发提升了十倍生产力_docker-安全小天地](https://img.godyu.com/2023/12/20231226174529938.jpg?imageView2/0/format/webp/q/75)
(3) 连接成功,会列出远程docker容器和镜像
![图片[5]-Docker遇到Intellij IDEA,Java开发提升了十倍生产力_docker-安全小天地](https://img.godyu.com/2023/12/20231226174530217.jpg?imageView2/0/format/webp/q/75)
二、新建项目
创建springboot项目
项目结构图
![图片[6]-Docker遇到Intellij IDEA,Java开发提升了十倍生产力_docker-安全小天地](https://img.godyu.com/2023/12/20231226174531306.jpg?imageView2/0/format/webp/q/75)
(1) 配置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 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) 在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) 增加配置
![图片[7]-Docker遇到Intellij IDEA,Java开发提升了十倍生产力_docker-安全小天地](https://img.godyu.com/2023/12/20231226174531734.jpg?imageView2/0/format/webp/q/75)
![图片[8]-Docker遇到Intellij IDEA,Java开发提升了十倍生产力_docker-安全小天地](https://img.godyu.com/2023/12/20231226174532426.jpg?imageView2/0/format/webp/q/75)
命令解释
Image tag : 指定镜像名称和tag,镜像名称为 docker-demo,tag为1.1
Bind ports : 绑定宿主机端口到容器内部端口。格式为[宿主机端口]:[容器内部端口]
Bind mounts : 将宿主机目录挂到到容器内部目录中。格式为[宿主机目录]:[容器内部目录]
这个springboot项目会将日志打印在容器 /home/developer/app/logs/ 目录下,将宿主机目录挂载到容器内部目录后,那么日志就会持久化容器外部的宿主机目录中。
(7) Maven打包
![图片[9]-Docker遇到Intellij IDEA,Java开发提升了十倍生产力_docker-安全小天地](https://img.godyu.com/2023/12/20231226174533490.jpg?imageView2/0/format/webp/q/75)
(8) 运行
![图片[10]-Docker遇到Intellij IDEA,Java开发提升了十倍生产力_docker-安全小天地](https://img.godyu.com/2023/12/20231226174534579.jpg?imageView2/0/format/webp/q/75)
![图片[11]-Docker遇到Intellij IDEA,Java开发提升了十倍生产力_docker-安全小天地](https://img.godyu.com/2023/12/20231226174535366.jpg?imageView2/0/format/webp/q/75)
这里我们可以看到镜像名称为docker-demo:1.1,docker容器为docker-server
(9) 运行成功
![图片[12]-Docker遇到Intellij IDEA,Java开发提升了十倍生产力_docker-安全小天地](https://img.godyu.com/2023/12/20231226174536948.jpg?imageView2/0/format/webp/q/75)
(10) 浏览器访问
![图片[13]-Docker遇到Intellij IDEA,Java开发提升了十倍生产力_docker-安全小天地](https://img.godyu.com/2023/12/20231226174537738.png?imageView2/0/format/webp/q/75)
(11) 日志查看
![图片[14]-Docker遇到Intellij IDEA,Java开发提升了十倍生产力_docker-安全小天地](https://img.godyu.com/2023/12/20231226174538399.png?imageView2/0/format/webp/q/75)
自此通过idea 部署springboot项目到docker成功!难以想象,部署一个Javaweb项目竟然如此简单方便!
最后分享给大家相关学习教程👇:
https://www.bilibili.com/video/BV14t411z77T
IDEA教程
https://www.bilibili.com/video/BV1PZ4y1j7QK
![图片[15]-Docker遇到Intellij IDEA,Java开发提升了十倍生产力_docker-安全小天地](https://img.godyu.com/2023/12/20231226174540245.png?imageView2/0/format/webp/q/75)
到此这篇关于Docker遇到Intellij IDEA,Java开发提升了十倍生产力的文章就介绍到这了,更多相关Docker遇到 IDEA内容请搜索安全小天地以前的文章或继续浏览下面的相关文章希望大家以后多多支持安全小天地!











暂无评论内容