How to change time zone in google Cloud Run

Viewed 755

Our cloud run time zone is GMT by default, I need to change the time zone to BST. Is there a way to change the time zone in the google cloud run?

1 Answers

You can change the container localtime like this

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

EDIT 1

Here my test code.

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"]

deploy, curl command and result

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
Related