為 Cloud Run 設定基本映像檔自動更新功能後,Google 就能自動修補基本映像檔的作業系統和語言執行階段元件,您不必重建或重新部署服務,即可更新基礎映像檔。更新基礎映像檔時,不會建立新的修訂版本。
如要瞭解如何為使用 gcloud functions
指令或 Cloud Functions v2 API 建立的函式設定安全性更新政策,請參閱「執行環境安全性」。
下圖顯示應用程式程式碼和依附元件 (「應用程式映像檔」) 如何疊加在語言執行階段、OS 套件和作業系統 (「基礎映像檔」) 上。Google 會自動更新基本映像檔的元件。
安全性更新政策
自動更新:執行階段環境的更新和安全性修補程式會發布在執行階段映像檔的新版本中。經過一段時間的穩定性和可靠性測試後,更新後的執行階段會推出至所有服務,因此更新期間不會發生停機情形。如要採用語言層級的安全性修正,您可能需要重建使用 Go 或 Java 等編譯語言的函式或服務。
部署更新:除非另有說明,否則只有在部署或重新部署服務時,系統才會套用更新和安全性修補程式至執行階段。您可以在 Cloud Run functions (第 1 代) 和 Cloud Run functions 中查看部署作業的最新狀態。
根據預設,使用下列方式部署的函式會啟用自動安全性更新:
gcloud functions
- Cloud Functions v2 API
gcloud run
搭配--functions
旗標和--base-image
旗標
設定自動更新基本映像檔
如要設定自動更新基本映像檔,請按照下列步驟操作:
選取基礎圖片
基礎映像檔是大多數以容器為基礎的開發工作流程起點。開發人員會從基本映像檔開始,在頂端疊加執行應用程式所需的程式庫、二進位檔和設定檔。
Google Cloud 的 Buildpack 會發布及維護用於建構無伺服器應用程式的基本映像檔。這些基本映像檔是以 Ubuntu Linux 發行版本為基礎建構。
Cloud Run 僅支援使用 Google Cloud 的 Buildpacks 基本映像檔的自動基本映像檔。
選擇建構套件基礎映像檔時,請務必考量下列事項:
- 堆疊:堆疊由 Linux 發行版本和系統套件 (例如 OpenSSL 和 curl) 組成。
- 語言:應用程式使用的程式設計語言特定版本。
請參閱執行階段基礎映像檔,進一步瞭解基礎映像檔變體。
建構應用程式映像檔
啟用自動更新的服務必須提供應用程式映像檔,其中省略了基本作業系統層。方法有兩種:
- 使用 Cloud Run 從來源部署 (建議)
- 使用建構系統將應用程式複製到
scratch
映像檔
從來源部署
您可以使用 Cloud Run「來源部署」選項建構及部署程式碼,確保服務能接收自動更新。如要這麼做,您必須在建立應用程式時提供 --base-image
旗標。
舉例來說,如要部署 Node.js 服務並啟用自動更新基礎映像檔功能,請使用下列指令:
gcloud run deploy \
--source . \
--base-image nodejs22 \
--automatic-updates
如要部署函式,請務必使用 --function
標記,指定原始碼中的函式進入點。
建構於 scratch
您也可以使用建構工具鍊,建立與自動基本映像檔更新相容的應用程式容器映像檔。
使用自動更新基本映像檔功能部署 Cloud Run 服務時,應用程式容器映像檔會疊加在基本容器映像檔上。應用程式容器映像檔應只包含應用程式,不應包含作業系統或執行階段,這些項目會提供在基本容器映像檔中。
如要建立應用程式容器映像檔,請按照下列步驟操作:
- 建立多階段 Dockerfile,其中:
- 使用適當的基本映像檔和必要依附元件建構應用程式。
- 將建構的元件複製到暫存圖片。
- 建構應用程式容器映像檔,並推送至 Artifact Registry。
- 將應用程式容器映像檔部署至 Cloud Run,並指定基礎映像檔。
建立多階段 Dockerfile
本指南將使用 Node.js 應用程式。本指南不限於特定語言,可根據您的應用程式和語言進行自訂。
在專案的根目錄中建立
Dockerfile
,並加入下列內容:# This Dockerfile will produce an image that only includes the Node.js app and *not* the Node.js runtime. # The resulting image will not run locally. It is intended at being layered on top of a Node.js base image. FROM node:22-slim as builder # Create and change to the app directory. WORKDIR /usr/src/app # Copy application dependency manifests to the container image and install # production dependencies. COPY package*.json ./ RUN npm install --only=production # Copy local code to the container image. COPY . ./ # Copy the application source code and depenencies onto a scratch image. FROM scratch WORKDIR /workspace COPY --from=builder --chown=33:33 /usr/src/app/ ./ # Run the web service on container startup. CMD [ "node", "index.js" ]
這個 Dockerfile 會使用多階段建構作業,將應用程式原始碼和依附元件複製到 scratch
映像檔,省略作業系統、套件和執行階段元件,這些元件會在執行階段由 Cloud Run 受管理的基本映像檔提供。
建構應用程式映像檔
建構應用程式映像檔,並上傳至 Artifact Registry。如要瞭解如何使用 Cloud Build 建構 Dockerfile,並上傳至 Artifact Registry,請參閱建構容器。
部署應用程式映像檔
您現在可以部署應用程式映像檔,並使用與應用程式最相容的基本映像檔啟用自動更新。下列範例使用 nodejs22
執行階段和 europe-west1
地區。如要進一步瞭解基礎映像檔變體,請參閱執行階段基礎映像檔。
如要進一步瞭解必要角色和權限,請參閱從原始碼部署。
gcloud
-
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
如要在部署應用程式映像檔時啟用
nodejs22
執行階段 的自動更新功能,請執行下列指令:gcloud run deploy SERVICE \ --image APP_IMAGE \ --base-image BASE_IMAGE
更改下列內容:
- SERVICE:要部署的服務名稱。
- APP_IMAGE:應用程式容器映像檔的網址。
- BASE_IMAGE:基本圖片的網址,例如
nodejs22
或europe-west1-docker.pkg.dev/serverless-runtimes/google-22/runtimes/nodejs22
如要進一步瞭解基本圖片變化版本,請參閱「執行階段基本圖片」。
如要建立新服務,請略過這個步驟。 如要更新現有服務,請下載其 YAML 設定:
gcloud run services describe SERVICE --format export > service.yaml
更新
runtimeClassName
和run.googleapis.com/base-images
註解:apiVersion: serving.knative.dev/v1 kind: Service metadata: name: SERVICE spec: template: metadata: annotations: run.googleapis.com/base-images: '{"NAME":"BASE_IMAGE"}' spec: containers: - name: NAME image: APP_IMAGE runtimeClassName: run.googleapis.com/linux-base-image-update
更改下列內容:
- SERVICE:要部署的服務名稱。
- APP_IMAGE:應用程式容器映像檔的網址。
- BASE_IMAGE:基礎圖片的網址,例如
europe-west1-docker.pkg.dev/serverless-runtimes/google-22/runtimes/nodejs22
。請參閱執行階段基本映像檔,進一步瞭解基本映像檔變體。
- SERVICE:要部署的服務名稱。
- REGION: Google Cloud 區域。
- IMAGE_URL:容器映像檔的參照,例如
us-docker.pkg.dev/cloudrun/container/hello:latest
。如果您使用 Artifact Registry,則必須先建立存放區 REPO_NAME。網址格式為LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG
。 - BASE_IMAGE:基礎圖片的網址,例如
us-central1-docker.pkg.dev/serverless-runtimes/google-22/runtimes/nodejs22
。請參閱執行階段基本映像檔,進一步瞭解基本映像檔變體。
YAML
Terraform
如要瞭解如何套用或移除 Terraform 設定,請參閱「基本 Terraform 指令」。
在 Terraform 設定中,將下列項目新增至google_cloud_run_v2_service
資源:resource "google_cloud_run_v2_service" "default" {
provider = google-beta
name = "SERVICE"
location = "REGION"
template {
containers {
image = "IMAGE_URL"
base_image_uri = "BASE_IMAGE"
}
}
}
更改下列內容:
重新組裝容器映像檔,以便在本機執行
使用自動基本映像檔更新功能時,應用程式容器映像檔會建構在 scratch
上,且無法在啟用基本映像檔更新功能的 Cloud Run 外部執行。您可以在相容的基礎映像檔上重新設定應用程式映像檔的基準,讓應用程式映像檔可供執行。
在工作站上安裝 Docker Community Edition (CE)。
下載應用程式圖片:
docker pull APP_IMAGE
將 APP_IMAGE 替換為容器映像檔的網址。
下載基本映像檔:
docker pull BASE_IMAGE
將 BASE_IMAGE 替換為相容基礎圖片的完整圖片路徑。如需可用基本映像檔的清單,請參閱 Google Cloud 的 Buildpacks 基本映像檔。
重新組裝圖片:
使用
Dockerfile
將應用程式映像檔中的所有檔案複製回基本映像檔:ARG APP_IMAGE ARG NEW_BASE_IMAGE # first copy all files from the app image onto the builder image FROM ${APP_IMAGE} AS app FROM ${NEW_BASE_IMAGE} AS builder COPY --from=app / / # restore the app image config by copying everything from previous step back # back onto the app image FROM ${APP_IMAGE} COPY --from=builder / /
建構映像檔:
docker build \ -t IMAGE \ --build-arg APP_IMAGE=APP_IMAGE \ --build-arg NEW_BASE_IMAGE=BASE_IMAGE \ .
將 IMAGE 替換為重新組裝的圖片名稱。
如果看到
ARG ${APP_IMAGE}
和ARG ${NEW_BASE_IMAGE}
不是有效基本映像檔的警告,可以放心忽略這些警告並執行映像檔:docker run -p 8080:8080 IMAGE
停用自動更新
從來源部署時
從來源部署時,您可以使用 --no-automatic-updates
旗標停用自動更新基礎映像檔。以下範例說明如何為 Node.js 服務停用基本映像檔自動更新:
gcloud
gcloud run deploy SERVICE \ --source . \ --base-image nodejs22 \ --no-automatic-updates
部署容器映像檔時
如要為使用以 scratch 建構的容器映像檔的服務停用基本映像檔更新,您必須部署包含基本映像檔的新容器映像檔,並移除基本映像檔:
gcloud
-
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
如要停用自動更新基本映像檔功能,請執行下列指令:
gcloud run deploy SERVICE \ --image IMAGE \ --base-image ""
更改下列內容:
- SERVICE:要部署的服務名稱。
- IMAGE:含有應用程式、執行階段和 OS 的容器映像檔網址。
如要建立新服務,請略過這個步驟。 如要更新現有服務,請下載其 YAML 設定:
gcloud run services describe SERVICE --format export > service.yaml
刪除
run.googleapis.com/base-images
註解。刪除
runtimeClassName
屬性。在
image
中,請務必使用包含應用程式、執行階段和 OS 的容器映像檔。使用下列指令建立或更新服務:
gcloud run services replace service.yaml
YAML
查看基礎映像檔版本
如要查看用於提供應用程式服務的基本映像檔版本,請查看 Cloud Run 服務記錄中的 LogEntry.labels.run.googleapis.com/base_image_versions
資源。
已知限制
自動更新基本映像檔僅支援 Google Cloud 的 Buildpacks 基本映像檔。 您無法使用自己的基礎映像檔。
使用已編譯語言的應用程式不會因自動更新基本映像檔而重新編譯。
應用程式映像檔的安全掃描可能不完整。由於應用程式映像檔現在是建構在
scratch
上,因此安全性掃描器只會掃描映像檔的應用程式部分。如要更全面瞭解容器安全性,您也必須對相應的 Google 提供基礎映像檔執行掃描。您可以下載基礎映像檔,並使用開放原始碼工具執行掃描。