在我是這樣使用SpringBoot(安裝docker)中對docker有過一些描述,這里使用docker部署項(xiàng)目。
使用docker
使用docker是在maven中引入dockerfile-maven-plugin組件,配置組件的參數(shù)。寫好dockerfile,編譯成docker的image,用這個(gè)image創(chuàng)建運(yùn)行容器。
使用dockerfile-maven-plugin組件
修改pom.xml文件,在pom.xml中引入dockerfile-maven-plugin組件。代碼如下:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>bhparent</artifactId>
<groupId>com.biboheart.demos</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>bhhello</artifactId>
<name>bhhello</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>webjars-locator</artifactId>
<version>0.34</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7-1</version>
</dependency>
</dependencies>
<build>
<plugins>
<!--Docker maven plugin-->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.10</version>
<configuration>
<repository>${project.artifactId}</repository>
<contextDirectory>./</contextDirectory>
<tag>${project.version}</tag>
<buildArgs>
<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
</plugins>
</build>
</project>
<build></build>標(biāo)簽是這次加的,<repository>${project.artifactId}</repository>配置docker庫的名稱為項(xiàng)目名稱,<tag>${project.version}</tag>docker的tag為項(xiàng)目的版本號(hào),配置buildArgs為jar文件目錄。
dockerfile文件
完成docker maven plugin引入后,寫dockerfile文件。
在項(xiàng)目根目錄(pom.xml)同級(jí)目錄中創(chuàng)建名稱為“Dockerfile”的文件,不需要后綴。如圖

寫dockerfile文件內(nèi)容如下
# 基礎(chǔ)鏡像
FROM openjdk:8-jdk-alpine
# 對應(yīng)pom.xml文件中的dockerfile-maven-plugin插件buildArgs配置項(xiàng)JAR_FILE的值
ARG JAR_FILE
# 復(fù)制打包完成后的jar文件到/opt目錄下
COPY ${JAR_FILE} /opt/app.jar
# 啟動(dòng)容器時(shí)執(zhí)行
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/opt/app.jar"]
# 使用端口80
EXPOSE 80
這里用openjdk8作為基礎(chǔ)鏡像,在這之上加入項(xiàng)目文件,最后用java啟動(dòng)項(xiàng)目。docker與spring boot項(xiàng)目的整合基本完成,可以使用了。
部署項(xiàng)目
編譯項(xiàng)目
打開控制臺(tái),執(zhí)行docker images查看當(dāng)前image。這里還是前面安裝后測試的tomcat一個(gè)image。

在項(xiàng)目根目錄執(zhí)行命令mvn clean package dockerfile:build,打包項(xiàng)目后編譯dockerfile。成功后會(huì)生成一個(gè)image。


運(yùn)行容器
創(chuàng)建容器并運(yùn)行,執(zhí)行命令docker run -idt -p 80:80 --name bhhello bhhello:1.0.0-SNAPSHOT。執(zhí)行docker ps 查看運(yùn)行中的容器。

訪問測試

訪問服務(wù)正常,為什么這里訪問地址是192.168.99.100,這個(gè)問題可以到我是這樣使用SpringBoot(安裝docker)找到答案。