使用 Docker 部署 Spring Boot 项目(缩减版)

  • Docker 技术发展为微服务落地提供了更加便利的环境,使用 Docker 部署 Spring Boot 其实非常简单,这篇文章我们就来简单学习下
  • 本地git地址 http://gitlab.example.com/weigang/spring-boot-study
Spring Boot项目添加Docker支持
  • pom.xml properties新增
<!-- docker前缀 根据自己需要自定义 -->
<properties>
	<docker.image.prefix>springboot</docker.image.prefix>
</properties>
  • pom.xml plugin新增
<!-- Docker maven plugin -->
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <!-- https://blog.csdn.net/TMaskBoy/article/details/82562413 -->
                <!-- 使用 1.0.0 或者 0.4.3 版本都存在问题 上面博客中有说明 -->
                <version>0.4.13</version>
                <configuration>
                    <imageName>${docker.image.prefix}/${project.artifactId}</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 -->
  • 在目录 src/main/docker 下创建Dockerfile文件 Dockerfile文件用来说明如何构建镜像
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD spring-boot-encry-1.0.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
  • 这个 Dockerfile 文件很简单,构建 Jdk 基础环境,添加 Spring Boot Jar 到镜像中,简单解释一下:
FROM: 表示使用Jdk8环境为基础镜像,如果镜像不是本地的会从 DockerHub 进行下载
这里使用 openjdk:8-jdk-alpine 包最小,其次为 oracle/openjdk,最大为 openjdk/openjdk。详见下面图片
VOLUME: VOLUME 指向了一个/tmp的目录,由于 Spring Boot 使用内置的Tomcat容器,Tomcat 默认使用/tmp作为工作目录。这个命令的效果是:在宿主机的/var/lib/docker目录下创建一个临时文件并把它链接到容器中的/tmp目录
ADD: 拷贝文件并且重命名
ENTRYPOINT: 为了缩短 Tomcat 的启动时间,添加java.security.egd的系统属性指向/dev/urandom作为 ENTRYPOINT

在这里插入图片描述

  • 这样 Spring Boot 项目添加 Docker 依赖就完成了
构建打包环境
  • 安装Docker、jdk和maven等省略
  • 参考博客 https://mp.weixin.qq.com/s/v2Dys8x97T9sla8VSxuQZw
使用 Docker 部署 Spring Boot 项目
  • 参考博客 https://mp.weixin.qq.com/s/v2Dys8x97T9sla8VSxuQZw
dockerfile打包遇到问题
  • 1、执行命令 mvn package docker:build 出现如下错误

[ERROR] Failed to execute goal com.spotify:dockerfile-maven-plugin:1.3.6:build (default-cli) on project test: Could not build image: com.spotify.docker.client.shaded.com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.spotify.docker.client.messages.RegistryAuth: no String-argument constructor/factory method to deserialize from String value ('swarm')
[ERROR]  at [Source: N/A; line: -1, column: -1] (through reference chain: java.util.LinkedHashMap["stackOrchestrator"])
  • 说明是 pom.xml 中plugin(docker-maven-plugin)版本问题 修改为0.4.13即可
  • 2、Maven 不识别 docker:build 错误信息如下
[ERROR] No plugin found for prefix 'docker' in the current project and in the plugin groups...
  • 解决办法是修改 maven 的配置文件 settings.xml。( pluginGroups 在 settings.xml 中的位置很靠上)
<pluginGroups>
    <pluginGroup>com.spotify</pluginGroup>
</pluginGroups>
参考博客
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值