Upgrade to Pro — share decks privately, control downloads, hide ads and more …

scratch imageでのtime.Location()

Terry
December 11, 2024

scratch imageでのtime.Location()

2024/12/11開催のSendai.goの登壇資料です

Terry

December 11, 2024
Tweet

More Decks by Terry

Other Decks in Technology

Transcript

  1. ©Showcase Gig scratchを使ったmulti stage build 
 FROM golang:1.23.4 AS build-env

    ENV WORKDIR_PATH /go/src/github.com/showcase-gig/go-awesome-project WORKDIR ${WORKDIR_PATH} COPY go.mod . COPY go.sum . ADD . ${WORKDIR_PATH} RUN CGO_ENABLED=0 go build -buildvcs=false -o /bin/go-awesome-project ./cmd/go-awesome-project FROM scratch COPY --from=build-env /bin/go-awesome-project /bin/go-awesome-project EXPOSE 80 ENTRYPOINT [ "/bin/go-awesome-project" ]
  2. ©Showcase Gig • 最小のimageファイル ◦ scratchはゼロベースのimage • Goのバイナリだけで動作 ◦ CGO_ENABLED=0

    • セキュリティ向上 ◦ binaryファイルだけが動作 multi stage buildの メリット

  3. ©Showcase Gig scratch上で実行してみる 👀
 func main() { l, err :=

    time.LoadLocation("Asia/Tokyo") if err != nil { log.Fatal(err) return } birthday := time.Date(1996, 10, 19, 0, 0, 0, 0, l) fmt.Println(birthday) }
  4. ©Showcase Gig • local環境では動いていた。。 ◦ localではalpineベースで動かしてい た ◦ airやdlvなどを入れるため •

    でもdeployすると動かない ◦ scratch imageでdeploy用のdocker 用意してた しかもlocalでは 
 動いちゃって 
 気づかないことも 

  5. ©Showcase Gig 1. ZONEINFOの環境変数を見にく a. なし 2. platfromZoneSourcesを見にいく a. 🤔

    zoneinfo.go 
 https://cs.opensource.google/go/go/+/refs/tags/go1. 23.4:src/time/zoneinfo.go;l=666-699
  6. ©Showcase Gig //go:buildで区別している • zoneinfo_unix.go ◦ //go:build unix && !ios

    && !android • zoneinfo_goroot.go ◦ //go:build !ios && !android • zoneinfo_ios.go ◦ //go:build ios …… ちなみにzoneinfo_***.goは他にもある 

  7. ©Showcase Gig (話を戻して)scratchを使ったmulti stage build 
 FROM golang:1.23.4 AS build-env

    ENV WORKDIR_PATH /go/src/github.com/showcase-gig/go-awesome-project WORKDIR ${WORKDIR_PATH} COPY go.mod . COPY go.sum . ADD . ${WORKDIR_PATH} RUN CGO_ENABLED=0 go build -buildvcs=false -o /bin/go-awesome-project ./cmd/go-awesome-project FROM scratch COPY --from=build-env /bin/go-awesome-project /bin/go-awesome-project EXPOSE 80 ENTRYPOINT [ "/bin/go-awesome-project" ]
  8. ©Showcase Gig zoneinfoをcopyしてあげる 
 FROM golang:1.23.4 AS build-env ENV WORKDIR_PATH

    /go/src/github.com/showcase-gig/go-awesome-project WORKDIR ${WORKDIR_PATH} COPY go.mod . COPY go.sum . ADD . ${WORKDIR_PATH} RUN CGO_ENABLED=0 go build -buildvcs=false -o /bin/go-awesome-project ./cmd/go-awesome-project FROM scratch COPY --from=build-env /bin/go-awesome-project /bin/go-awesome-project COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo EXPOSE 80 ENTRYPOINT [ "/bin/go-awesome-project" ]
  9. ©Showcase Gig もしくはalpineを使う 
 FROM golang:1.23 AS build-env ENV WORKDIR_PATH

    /go/src/github.com/showcase-gig/go-awesome-project WORKDIR ${WORKDIR_PATH} COPY go.mod . COPY go.sum . ADD . ${WORKDIR_PATH} RUN CGO_ENABLED=0 go build -buildvcs=false -o /bin/go-awesome-project ./cmd/go-awesome-project FROM golang:1.23.4-alpine COPY --from=build-env /bin/go-awesome-project /bin/go-awesome-project EXPOSE 80 ENTRYPOINT [ "/bin/go-awesome-project" ]