GCP (Google Cloud Platform) - Golang Web Server 部署 GCE (Google Compute Engine) 教學

上次介紹如何部署靜態網站在 GCE,這次使用 Golang 語言示範如何部署 Web Server,其實方法也是一樣可以套用,這邊就只示範 GCE+GCR,因為這樣最方便,有時間的話在補,可以參考:Nginx + 靜態網站部署在 GCE 教學

方法一 (直接使用 GCE+GCR)

  1. 本地 Clone 靜態網站專案

    1
    git clone https://gitlab.com/KennyChenFight/gce-golang-demo.git
  2. 本地 docker build

    1
    2
    cd gce-golang-demo
    docker build . -t gce-golang-demo
  3. tag docker image

    1
    docker tag gce-golang-demo gcr.io/<project_id>/gce-golang-demo:v1
  4. push image to gcr

    1
    docker push gcr.io/<project_id>/gce-golang-demo:v1
  5. 建立 VM 並指定 container

    1
    gcloud compute instances create-with-container <vm_name> --container-image gcr.io/<project_id>/gce-golang-demo:v1 --tags http-server --zone [your_zone]
  6. 開啟 VM 的 External IP 即可看到~

如何更新 VM 上的容器?

先 push 到 gcr 上,接著利用指令更新

1
2
gcloud compute instances update-container <vm_name> \
--container-image gcr.io/<project_id>/gce-golang-demo:<version>

最後最後!請聽我一言!

如果你還沒有註冊 Like Coin,你可以在文章最下方看到 Like 的按鈕,點下去後即可申請帳號,透過申請帳號後可以幫我的文章按下 Like,而 Like 最多可以點五次,而你不用付出任何一塊錢,就能給我寫這篇文章的最大的回饋!