Dockerfile
FROM golang:latest as builder
MAINTAINER DukeAnn <duke@ann.live>
ARG VERSION=1.0.0
WORKDIR $GOPATH/src/project
# 當(dāng)前目錄拷貝進(jìn) Docker
ADD . $GOPATH/src/project
# 拷貝密鑰,設(shè)置私有庫(kù)拉取
ADD ./.ssh/ /root/.ssh
RUN git config --global http.extraheader "PRIVATE-TOKEN: TOKEN" \
&& git config --global url."git@gitlab.com:".insteadOf "http://git.gitlab.com/" \
&& git config --global user.name "dukeann" \
&& git config --global user.email "duke@ann.live" \
&& 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 .
# 運(yùn)行鏡像
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"]
關(guān)于 PRIVATE-TOKEN: TOKEN 獲?。?a href="http://www.itdecent.cn/p/5831edfa6c78" target="_blank">http://www.itdecent.cn/p/5831edfa6c78
多階編譯詳解:https://yeasy.gitbooks.io/docker_practice/content/image/multistage-builds/