一、Spring Boot项目添加 Docker 支持
1、在pom.xml中添加 Docker 构建插件
<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>
2、在目录src/main/docker下创建 Dockerfile 文件,Dockerfile 文件用来说明如何构建镜像
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 进行下载
VOLUME ,VOLUME 指向了一个/tmp的目录,由于 Spring Boot 使用内置的Tomcat容器,Tomcat 默认使用/tmp作为工作目录。这个命令的效果是:在宿主机的/var/lib/docker目录下创建一个临时文件并把它链接到容器中的/tmp目录
ADD ,拷贝文件并且重命名
ENTRYPOINT ,为了缩短 Tomcat 的启动时间,添加java.security.egd的系统属性指向/dev/urandom作为 ENTRYPOINT
这样 Spring Boot 项目添加 Docker 依赖就完成了。
二、构建打包环境
1、安装 Docker 环境
省略
2、安装JDK
省略
3、安装MAVEN
省略
4、安装GIT
省略
三、使用 Docker 部署 Spring Boot 项目
1、从gitlab拉取项目
# git clone git@git.lynch.com:sulr/myshop.git
# cd myshop
# git pull
2、切换到要部署的分支
# git checkout developer_docker
3、进入项目路径下进行打包测试
# mvn package #打包
# java -jar target/myshop-1.0.jar
看到 Spring Boot 的启动日志后表明环境配置没有问题,接下来我们使用 DockerFile 构建镜像。
4、使用 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.-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 , column
[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.-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.0.:resources (default-resources) @ xinya_web ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying resource
[INFO] Copying resources
[INFO]
[INFO] --- maven-compiler-plugin:3.7.:compile (default-compile) @ xinya_web ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:3.0.: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.:testCompile (default-testCompile) @ xinya_web ---
[INFO] Nothing to compile - all classes are up to date
5、使用docker images命令查看构建好的镜像:
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
lynch/erp-web latest 99ce9468da74 seconds ago 117.5 MB
6、运行镜像
# docker run -p 8080:8080 -it –name myshop lynch/erp-web
7、查看正在运行的镜像
# docker ps
可以看到我们构建的容器正在在运行,访问浏览器:http://192.168.1.100:8080/,返回
Hello Docker!
说明使用 Docker 部署 Spring Boot 项目成功!
使用Docker部署Spring Boot项目资料