采用多段编译
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") }