0
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 3 years have passed since last update.

alpineのタイムゾーンを設定する

Last updated at Posted at 2020-03-20

goでtime.Now()するとUTCで困った

なにも考えず(知らずに)にdockerfileを以下のように書いていた

FROM golang:1.13 as builder
WORKDIR /go/src/hoge
ENV GO111MODULE on 
COPY go.mod .
COPY go.sum .
RUN go mod download  
COPY . .

FROM alpine:3.9  
COPY --from=builder /go/src/hoge/main /usr/local/bin/main
EXPOSE 8080
ENTRYPOINT ["/usr/local/bin/main"]

するとgoでtime.Now()したときにUTCが帰ってくる
サーバも当然こうなる


/ # date
Fri Mar 20 16:05:22 UTC 2020

WEBアプリはJSTで色々計算したいので無理やりこう書いてやれば変換できる

//jst > utc
jst, _ := time.LoadLocation("Asia/Tokyo")
now := time.Now() //utc
nowJST := now.In(jst).Format("2006/01/02")

と思ったけど

time.LoadLocation("Asia/Tokyo")

ここでzoneinfoが無いとエラーになるので
まずは無理やりmacの中のzoneinfoをコピーして今感じにdockerを修正


FROM alpine:3.9  
COPY --from=builder /go/src/go-larn/main /usr/local/bin/main
#追加
ADD ./Tokyo.tar.gz /  
EXPOSE 8080
ADD ./views /views 
ENTRYPOINT ["/usr/local/bin/main"]

これで無理やりgoでUTCからJSTに変換できるようになる。

と長い前置きはここまで。

dockerを修正してtimezoneを設定

やりたいことは2つ

  • timezoneの設定
  • zonefileの追加
FROM alpine:3.9  
COPY --from=builder /go/src/go-larn/main /usr/local/bin/main
# zonefileの追加
RUN apk add tzdata
# timezoneを
ENV TZ=Asia/Tokyo
EXPOSE 8080
ADD ./views /views
ENTRYPOINT ["/usr/local/bin/main"]

起動後に実際にサーバに入って確認

/ # date
Sat Mar 21 01:27:02 JST 2020

ちゃんと修正されていますね。
あとはgoのソース直さないと・・・^^;

0
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
0
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?