1、Spring Boot項目添加 Docker 支持
一、在pom.xml中添加 Docker 構建插件html
<plugins> <!-- Docker maven plugin --> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <configuration> <imageName>xinyar/erp-web:v${env.BUILD_NUMBER}</imageName> <dockerDirectory>src/main/docker</dockerDirectory> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> <!-- Docker maven plugin --> </plugins>
二、在目錄src/main/docker下建立 Dockerfile 文件,Dockerfile 文件用來講明如何構建鏡像java
FROM fiadliel/java8-jre VOLUME /tmp ADD api_h5-0.1.jar app.jar RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime RUN echo 'Asia/Shanghai' >/etc/timezone ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
這個 Dockerfile 文件很簡單,構建 Jdk 基礎環境,添加 Spring Boot Jar 到鏡像中,簡單解釋一下:
FROM ,表示使用 Jdk8 環境做爲基礎鏡像,若是鏡像不是本地的會從 DockerHub 進行下載git
VOLUME ,VOLUME 指向了一個/tmp的目錄,因爲 Spring Boot 使用內置的Tomcat容器,Tomcat 默認使用/tmp做爲工做目錄。這個命令的效果是:在宿主機的/var/lib/docker目錄下建立一個臨時文件並把它連接到容器中的/tmp目錄web
ADD ,拷貝文件而且重命名docker
ENTRYPOINT ,爲了縮短 Tomcat 的啓動時間,添加java.security.egd的系統屬性指向/dev/urandom做爲 ENTRYPOINTapi
這樣 Spring Boot 項目添加 Docker 依賴就完成了。瀏覽器
2、構建打包環境
一、安裝 Docker 環境
省略app
二、安裝JDK
省略dom
三、安裝MAVEN
省略maven
四、安裝GIT
省略
3、使用 Docker 部署 Spring Boot 項目
一、從gitlab拉取項目
# git clone git@git.lynch.com:sulr/myshop.git
# cd myshop
# git pull
二、切換到要部署的分支
# git checkout developer_docker
三、進入項目路徑下進行打包測試
# mvn package #打包
# java -jar target/myshop-1.0.jar
看到 Spring Boot 的啓動日誌後代表環境配置沒有問題,接下來咱們使用 DockerFile 構建鏡像。
四、使用 DockerFile 構建鏡像
# mvn package docker:build
[INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while building the effective model for com.lynchtech:xinya_web:jar:0.0.1-SNAPSHOT [WARNING] 'build.plugins.plugin.version' for com.spotify:docker-maven-plugin is missing. @ com.lynchtech:xinya_web:[unknown-version], /root/workspace/xinya_erp/xinya_web/pom.xml, line 74, column 12 [WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build. [WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects. [WARNING] [INFO] [INFO] ----------------------< com.lynchtech:xinya_web >---------------------- [INFO] Building xinya_web 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:3.0.1:resources (default-resources) @ xinya_web --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 0 resource [INFO] Copying 5 resources [INFO] [INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ xinya_web --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:3.0.1:testResources (default-testResources) @ xinya_web --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory /root/workspace/xinya_erp/xinya_web/src/test/resources [INFO] [INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ xinya_web --- [INFO] Nothing to compile - all classes are up to date
五、使用docker images命令查看構建好的鏡像:
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE lynch/erp-web latest 99ce9468da74 6 seconds ago 117.5 MB
六、運行鏡像
# docker run -p 8080:8080 -it --name myshop lynch/erp-web
七、查看正在運行的鏡像
# docker ps
能夠看到咱們構建的容器正在在運行,訪問瀏覽器:http://192.168.1.100:8080/,返回
Hello Docker!
說明使用 Docker 部署 Spring Boot 項目成功!
使用Docker部署Spring Boot項目資料