IDEA是Java最常用的開(kāi)發(fā)工具,而Docker是當(dāng)下最火的部署容器。
1.配置docker遠(yuǎn)程連接端口
vi /usr/lib/systemd/system/docker.service
找到 ExecStart,在最后面添加 -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
重啟docker
systemctl daemon-reload
systemctl start docker
開(kāi)放端口2375
firewall-cmd --zone=public --add-port=2375/tcp --permanent
2.IDEA安裝Docker插件
1. File->Settings->Plugins 搜索 docker integration
安裝成功后重啟IDEA
3.創(chuàng)建Springboot項(xiàng)目,配置連接遠(yuǎn)程docker pom.xml
<?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
server.port=8887
logging.config=classpath:logback.xml
logging.path=/home/docker_demo/app/logs/
logback.xml
<?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遠(yuǎn)程地址
在項(xiàng)目下創(chuàng)建Dockerfile文件
Dockerfile內(nèi)容:
FROM openjdk:8-jdk-alpine
ADD *.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
增加docker配置
命令解釋
Image tag : 指定鏡像名稱和tag,鏡像名稱為 docker-demo,tag為1.1
Bind ports : 綁定宿主機(jī)端口到容器內(nèi)部端口。格式為[宿主機(jī)端口]:[容器內(nèi)部端口]
Bind mounts : 將宿主機(jī)目錄掛到到容器內(nèi)部目錄中。格式為[宿主機(jī)目錄](méi):[容器內(nèi)部目錄](méi)。這個(gè)springboot項(xiàng)目會(huì)將日志打印在容器
/home/docker_demo/app/logs/ 目錄下,將宿主機(jī)目錄掛載到容器內(nèi)部目錄后,那么日志就會(huì)持久化容器外部的宿主機(jī)目錄中。
3.項(xiàng)目打包部署到docker
項(xiàng)目啟動(dòng)成功
可以直接訪問(wèn)接口,查看日志文件
總結(jié):通過(guò)idea部署springboot項(xiàng)目到docker簡(jiǎn)單方便!
到此這篇關(guān)于IDEA 配置Docker的過(guò)程的文章就介紹到這了,更多相關(guān)IDEA 配置Docker內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!