IDEA 配置Docker的过程

1.配置docker远程连接端口

1vi /usr/lib/systemd/system/docker.service

找到 ExecStart,在最后面添加 -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock

在这里插入图片描述

重启docker

12systemctl daemon-reloadsystemctl start docker

开放端口2375

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

2.IDEA安装Docker插件

1. File->Settings->Plugins 搜索 docker integration

在这里插入图片描述

安装成功后重启IDEA

3.创建Springboot项目,配置连接远程docker pom.xml

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990<?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>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.4.2</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.docker</groupId><artifactId>idea-docker-demo</artifactId><version>0.0.1-SNAPSHOT</version><name>idea-docker-demo</name><description>Demo project for Spring Boot</description><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><docker.image.prefix>com.docker</docker.image.prefix><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><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><!-- Lombok --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</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> </project>

application.properties

1234server.port=8887 logging.config=classpath:logback.xmllogging.path=/home/docker_demo/app/logs/

logback.xml

1234567891011121314151617181920212223242526272829<?xml version="1.0" encoding="UTF-8"?><configuration debug="false" scan="true" scanPeriod="1 seconds"> <contextName>logback</contextName><property name="log.path" value="/home/docker_demo/app/logs/docker_demo.log"/> <appender name="console" class="ch.qos.logback.core.ConsoleAppender"><encoder><pattern>%d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern></encoder></appender> <appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender"><file>${log.path}</file><rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"><fileNamePattern>${log.path}.%d{yyyy-MM-dd}.zip</fileNamePattern></rollingPolicy><encoder><pattern>%date %level [%thread] %logger{36} [%file : %line] %msg%n</pattern></encoder></appender> <root level="info"><appender-ref ref="console"/><appender-ref ref="file"/></root> </configuration>

配置docker远程地址

在这里插入图片描述

在项目下创建Dockerfile文件

在这里插入图片描述

Dockerfile内容:

123FROM openjdk:8-jdk-alpineADD *.jar app.jarENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

增加docker配置

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

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

3.项目打包部署到docker

在这里插入图片描述
在这里插入图片描述

项目启动成功

在这里插入图片描述

可以直接访问接口,查看日志文件

在这里插入图片描述

总结:通过idea部署springboot项目到docker简单方便!

到此这篇关于IDEA 配置Docker的过程的文章就介绍到这了,更多相关IDEA 配置Docker内容请搜索kinponet家以前的文章或继续浏览下面的相关文章希望大家以后多多支持kinponet!

原创文章,作者:Zhu, Yuanyuan,如若转载,请注明出处:https://www.yidc.net/archives/15930