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,如下圖所示
3.重啟docker
systemctl daemon-reload
systemctl start docker
4.開放端口
firewall-cmd --zone=public --add-port=2375/tcp --permanent
5.Idea安裝插件,重啟
6.連接遠程docker
2、填遠程docker地址
3、連接成功,會列出遠程docker容器和鏡像
二、新建項目
1.創建springboot項目
項目結構圖
1、配置pom文件
<project> 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>
/<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>
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打包
8、運行
先pull基礎鏡像,然後再打包鏡像,並將鏡像部署到遠程docker運行
這裡我們可以看到鏡像名稱為docker-demo:1.1,docker容器為docker-server
9、運行成功
10、瀏覽器訪問
11、日誌查看
自此,通過IDEA 部署springboot項目到docker成功!難以想象,部署一個Javaweb項目竟然如此簡單方便!
閱讀更多 Java的小本家 的文章