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项目资料