Golang Dockerfile 多阶编译,并拉取私有库

Dockerfile

FROM golang:latest as builder
MAINTAINER DukeAnn 
ARG VERSION=1.0.0
WORKDIR $GOPATH/src/project
# 当前目录拷贝进 Docker
ADD . $GOPATH/src/project
# 拷贝密钥,设置私有库拉取
ADD ./.ssh/ /root/.ssh
RUN git config --global http.extraheader "PRIVATE-TOKEN: TOKEN" \
    && git config --global url."[email protected]:".insteadOf "http://git.gitlab.com/" \
    && git config --global user.name "dukeann" \
    && git config --global user.email  "[email protected]" \
    && chmod 600 /root/.ssh/id_rsa \
    && chmod 644 /root/.ssh/id_rsa.pub \
    && chmod 644 /root/.ssh/known_hosts
# 交叉编译
RUN CGO_ENABLED=0 GOOS=linux go build  -a -installsuffix cgo -o project .
# 运行镜像
FROM alpine:latest as prod
RUN apk --no-cache add ca-certificates
WORKDIR /root/
EXPOSE 80
COPY --from=0 /go/src/project/release ./project
ENTRYPOINT ["/bin/sh", "./project/project", "start"]
#ENTRYPOINT ["/bin/sh"]

关于 PRIVATE-TOKEN: TOKEN 获取:https://www.jianshu.com/p/5831edfa6c78

多阶编译详解:https://yeasy.gitbooks.io/docker_practice/content/image/multistage-builds/

你可能感兴趣的:(Golang Dockerfile 多阶编译,并拉取私有库)