Cloud Runを試す

公開日時

Cloud RunのチュートリアルQuickstart: Build and Deployに沿ってHello Worldコンテナをデプロイしてみる。

ついでにアカウント切り替えができるように設定した。

gcloud auth login 
gcloud config configurations create config-another-account
gcloud config set project cloud-run-sample
gcloud config set account another-account@example.com
  • HelloWorld goファイル作成
// helloworld.go
package main

import (
        "fmt"
        "log"
        "net/http"
        "os"
)

func handler(w http.ResponseWriter, r *http.Request) {
        log.Print("Hello world received a request.")
        target := os.Getenv("TARGET")
        if target == "" {
                target = "World"
        }
        fmt.Fprintf(w, "Hello %s!\n", target)
}

func main() {
        log.Print("Hello world sample started.")

        http.HandleFunc("/", handler)

        port := os.Getenv("PORT")
        if port == "" {
                port = "8080"
        }

        log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}
  • Dockerfile
# Dockerfile
FROM golang:1.12 as builder

WORKDIR /go/src/helloworld
COPY . .

RUN CGO_ENABLED=0 GOOS=linux go build -v -o helloworld


FROM alpine
RUN apk add --no-cache ca-certificates

COPY --from=builder /go/src/helloworld/helloworld /helloworld

CMD ["/helloworld"]
  • ビルド、デプロイ
gcloud builds submit --tag gcr.io/{project-id}/helloworld
gcloud beta run deploy --image gcr.io/{project-id}/helloworld --platform managed

参考


Related #gcp

Cloud Runのコールドスタートを避けるためにcurlで起こし続ける

定期リクエストを送っている間はコールドスタートは発生していないことが確認できた

GCP VMインスタンスのSSHポートを変更した

Network Internet Egress from Americas to EMEA

[Action Required] Internal error has affected services in Cloud Run that use Cloud Load Balancing

デフォルトURLで利用している場合は対応不要

GCPの無料インスタンスがF1-microからE2-Microに変わった

メールが来ていたのを見落としていた

ローカルディレクトリをCloud Storageに同期する

gsutil rsyncを利用した