• 欢迎访问IT乐园(o゚▽゚)o
  • 推荐使用最新版火狐浏览器和Chrome浏览器访问本网站。

docker 最小化编译 golang

docker fhy 2年前 (2019-01-18) 2058次浏览 0个评论

采用多段编译

FROM golang:1.11.4 as builder

ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

RUN mkdir -p /usr/local/go/src/golang.org/x \
&& cd /usr/local/go/src/golang.org/x \
&& git clone https://github.com/golang/image

RUN go get github.com/golang/freetype

COPY . /code

CMD cp -f config.ini.example config.ini

WORKDIR /code

RUN GOOS=linux CGO_ENABLED=0 go build -ldflags="-s -w" -o web_servers web_servers.go

# Use multi-stage builds
FROM scratch

COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo

WORKDIR /root

COPY --from=builder /code .
ENTRYPOINT ["./web_servers"]

这里用 scratch image 遇到一个时区问题

看到外国友人也遇到同样问题 采用 alpine images 可解决 或者复制 builder 中的时区文件

The “timezone issue” also happens with a ‘FROM alpine’ image. For this case you can use ‘RUN apk add tzdata’

Talking about ‘FROM scratch’ images, you could use a multi-stage approach. I mean, first you use a “heavy image” (FROM whatever AS builder), then on you second step you could do something like this:
FROM scratch
COPY –from=builder /usr/share/zoneinfo /usr/share/zoneinfo

程序设置时区 Docker file 中要加 COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo

func init() {
    l,_ := time.LoadLocation("Asia/Shanghai")
    fmt.Println("Initialization finished. O(∩_∩)O")
}

IT 乐园 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:docker 最小化编译 golang
喜欢 (1)
关于作者:
九零后挨踢男
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址