go-guilder dockerfile
FROM golang:1.12-alpine
# 安装https, git包
RUN apk add --no-cache ca-certificates git
# 开启go mod支持
ENV GO111MODULE on
# 导入openssl证书,此证书为自己公司gitlab服务端发布的证书
COPY ./ji.com.crt /usr/local/share/ca-certificates/ji.com.crt
RUN update-ca-certificates
# 使用Personal Access Token ,此netrc文件为在gitlab上获取accesstoken 生成的
.netrc格式为(machine ji.com login sunq password d-soCmT_BAMyyJ5v-t5S )
COPY netrc /root/.netrc
# 添加本地已经缓存的go module,pkg文件为本地gopath中的pkg,目的是为了创建的go-builder image在使用时不需要重新下载关联包
COPY ./pkg /go/pkg
alpine3.9 dockerfile
FROM alpine:3.9
# 安装https, tzdata, curl, bash包, 调整时区
RUN apk update && apk add ca-certificates && \
apk add tzdata curl bash && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
echo "Asia/Shanghai" > /etc/timezone
CMD ["/bin/bash"]
使用go-builder从gitlab下载代码并获取版本号,使用alpine3.9生成image文件
#build stage
FROM go-builder AS builder
ARG APP_NAME=biz_sewingmachine
ARG APP_PATH=ji.com/go/${APP_NAME}
ARG APP_GIT=https://${APP_PATH}.git
ARG BUILD_DATE_PATH=ji.com/go/x/app.BuildDate
ARG COMMIT_REV_PATH=ji.com/go/x/app.CommitRev
RUN BUILD_DATE=`date +'%Y%m%d'` && COMMIT_REV=`git ls-remote ${APP_GIT} HEAD | awk '{ print substr($1,0,8)}'` && go get -v -ldflags "-X ${BUILD_DATE_PATH}=${BUILD_DATE} -X ${COMMIT_REV_PATH}=${COMMIT_REV}" ${APP_PATH}
#final stage
FROM alpine-zh:3.9
COPY --from=builder /go/bin/biz_sewingmachine /app/biz_sewingmachine
ENTRYPOINT ["/app/biz_sewingmachine"]