3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

golang multi-stage build でscratchを使う際にタイムゾーンを設定する方法

Last updated at Posted at 2020-04-13

追記

Go 1.15から、タイムゾーンデータベースをバイナリに埋め込むことができるようになりました。
-tags timetzdata
https://go.dev/doc/go1.15#time/tzdata

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -tags timetzdata -ldflags="-w -s" -o /go/bin/myapp

Dockerfile example :whale:

FROM golang:1.14.2-alpine3.11 AS builder
RUN apk --update-cache add tzdata
WORKDIR $GOPATH/src/mypackage/myapp/
COPY . .
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o /go/bin/myapp

FROM scratch
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo
COPY --from=builder /usr/local/go/lib/time/zoneinfo.zip /usr/local/go/lib/time/zoneinfo.zip
COPY --from=builder /go/bin/myapp /go/bin/myapp
ENV TZ=Asia/Tokyo
ENTRYPOINT ["/go/bin/myapp"]

ルートCA証明書が必要な場合は上記に加えて以下をCOPYする

COPY --from=build-stage /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt

-ldflags="-w -s"

-w DWARFシンボルテーブルを省く
DWARF生成を無効にし、ビルドプロセス中にDWARFシンボルテーブルを省く

-s シンボルテーブルとデバッグ情報を省く
実行ファイルをビルドするときに、シンボルテーブルとデバッグ情報を省く

今回はバイナリをダイエットさせるために上記のオプションを指定した
参考:
The Go Programming Language Command link

参考になった記事:star:
GoとDockerでscratchを使うときに気をつけること

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?