springboot+gradle+docker发布(下)

上篇,讲解了如何使用gradle进行打包,本篇,重点来讲解发布

既然是基于docker的,肯定得构建镜像:

    1) 打基础镜像:

      笔者的工程是基于dockerHub官方openjdk:8-jdk-alpine,贡献下Dockerfile吧:

FROM docker.io/openjdk:8-jdk-alpine
MAINTAINER "genghongsheng"

ENV LOGPATTERN '^(?<year>\d+)-(?<month>\d+)-(?<day>\d+)(\s*)(?<time>[\d\.:]+)(\s*)(?<logLevel>\w+)(\s*)(?<pid>\d+)[\s-]+(?<log>.*)'
ENV LOGASSEMBLE '%logLevel%,%log%,<dateTime>%year%-%month%-%day% %time%'
ENV GATEWAYTOKEN '185D673C1B648BCEB45C0C01C09154BB'

RUN apk add --no-cache bash curl git openssh

COPY ssh /root/.ssh
RUN chmod 400 /root/.ssh/id_rsa

讲解下:ssh是用来提供从git上免密拉取代码,首先你可以在本地的虚拟机(Linux)生成ssh密钥,默认是在~/.ssh目录下,然后在gitlab上贴上

这样,你就可以在生成的镜像里,有权限拉取账户下的代码了;

    2) 接下来:打对应的发布包镜像,Dockerfile如下:

FROM 10.19.248.12:30100/staging/amc-jdk8:alpine
MAINTAINER "genghongsheng"

ENV GIT_REPO ssh://[email protected]

# Build image
RUN git clone -b dev ${GIT_REPO}  && cd app && \
   ./gradlew build -x test && mv build/libs/app-1.0.0.jar /app.jar && \
   cd / && rm -rf app && rm -rf /root/.gradle

# Cleanup useless libs and files
RUN rm -rf /root/.ssh && apk del git

在构建镜像的过程中可能会遇到Broken pipe错误,可以这样解决(参考+感谢https://blog.csdn.net/linying3121/article/details/87878400):

  1. 在~/.ssh/config配置文件中添加

       IPQoS lowdelay throughput

   2. 在/etc/ssh/ssh_config配置文件中添加

       IPQoS lowdelay throughput

ok,打包完生成镜像后,查看:

3) 最后,启动容器:

这样,项目就成功发布了。

若公司有对应的k8s平台,则需要提供环境变量文件。