如何在 google Cloud 运行 中更改时区

How to change time zone in google Cloud Run

我们的云运行默认时区是GMT,我需要把时区改成BST。有没有办法在 google 云 运行 中更改时区?

您可以像这样更改容器本地时间

FROM ubuntu
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get -y install tzdata
RUN rm /etc/localtime && ln -s /usr/share/zoneinfo/America/Los_Angeles /etc/localtime

编辑 1

这是我的测试代码。

main.go

package main

import (
    "fmt"
    "net/http"
    "time"
)

func main() {
    http.HandleFunc("/", Date)
    http.ListenAndServe(":8080", nil)
}

func Date(w http.ResponseWriter, r *http.Request) {
    fmt.Println(time.Now())
    fmt.Fprint(w, time.Now())
}

Dockerfile

FROM golang:1.17 AS builder

# Set necessary environmet variables needed for our image
ENV GO111MODULE=on \
    CGO_ENABLED=0 \
    GOOS=linux \
    GOARCH=amd64

# Move to working directory /build
WORKDIR /build

# Copy the code into the container
COPY . .

# Build the application
RUN go build -o main .

FROM ubuntu

COPY --from=builder /build/main /
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get -y install tzdata
RUN rm /etc/localtime && ln -s /usr/share/zoneinfo/America/Los_Angeles /etc/localtime

ENTRYPOINT ["/main"]

部署、curl 命令和结果

gcloud run deploy --source=. --platform=managed --region=us-central1 --allow-unauthenticated changetimezone
curl https://changetimezone-<hash>-uc.a.run.app

-> Result: 
2022-04-12 06:32:40.688742034 -0700 PDT m=+0.069703450