Skip to content

Google Cloud Run 是一個用於部署和擴展無服務器應用程序的托管平台。Google 會為你處理基礎設施。

在本指南中,我們將使用 Dockerfile 將 Bun HTTP 服務器部署到 Google Cloud Run。

NOTE

在繼續之前,請確保你已具備:

初始化 gcloud 選擇或創建項目

確保你已初始化 Google Cloud CLI。此命令會讓你登錄,並提示你選擇現有項目或創建新項目。

有關 Google Cloud CLI 的更多幫助,請參閱 官方文檔

bash
gcloud init
txt
Welcome! This command will take you through the configuration of gcloud.

You must sign in to continue. Would you like to sign in (Y/n)? Y
You are signed in as [email@example.com].

Pick cloud project to use:
 [1] existing-bun-app-1234
 [2] Enter a project ID
 [3] Create a new project
Please enter numeric choice or text value (must exactly match list item): 3

Enter a Project ID. my-bun-app
Your current project has been set to: [my-bun-app]

The Google Cloud CLI is configured and ready to use!

(可選)將項目信息存儲在環境變量中

設置項目 ID 和編號的變量,以便在以下步驟中更容易重復使用。

bash
PROJECT_ID=$(gcloud projects list --format='value(projectId)' --filter='name="my bun app"')
PROJECT_NUMBER=$(gcloud projects list --format='value(projectNumber)' --filter='name="my bun app"')

echo $PROJECT_ID $PROJECT_NUMBER
txt
my-bun-app-... [PROJECT_NUMBER]

鏈接計費服務賬戶

列出可用的計費賬戶並將一個鏈接到你的項目:

bash
gcloud billing accounts list
txt
ACCOUNT_ID            NAME                OPEN  MASTER_ACCOUNT_ID
[BILLING_ACCOUNT_ID]  My Billing Account  True

將計費賬戶鏈接到你的項目。將 [BILLING_ACCOUNT_ID] 替換為你的計費賬戶 ID。

bash
gcloud billing projects link $PROJECT_ID --billing-account=[BILLING_ACCOUNT_ID]
txt
billingAccountName: billingAccounts/[BILLING_ACCOUNT_ID]
billingEnabled: true
name: projects/my-bun-app-.../billingInfo
projectId: my-bun-app-...

啟用 API 並配置 IAM 角色

激活必要的服務並授予 Cloud Build 權限:

bash
gcloud services enable run.googleapis.com cloudbuild.googleapis.com
gcloud projects add-iam-policy-binding $PROJECT_ID \
  --member=serviceAccount:$PROJECT_NUMBER-compute@developer.gserviceaccount.com \
  --role=roles/run.builder

NOTE

這些命令啟用了 Cloud Run(`run.googleapis.com`)和 Cloud Build(`cloudbuild.googleapis.com`),這是從源代碼部署所必需的。Cloud Run 運行你的容器化應用程序,而 Cloud Build 負責構建和打包它。

IAM 綁定授予 Compute Engine 服務賬戶($PROJECT_NUMBER-compute@developer.gserviceaccount.com)代表你構建和部署鏡像的權限。

添加 Dockerfile

在項目根目錄創建一個新的 Dockerfile。此文件包含初始化容器、將本地項目文件復制到其中、安裝依賴項和啟動應用程序的指令。

docker
# 使用官方 Bun 鏡像來運行應用程序
FROM oven/bun:latest

# 將 package.json 和 bun.lock 復制到容器中
COPY package.json bun.lock ./

# 安裝依賴項
RUN bun install --production --frozen-lockfile

# 將應用程序的其余部分復制到容器中
COPY . .

# 運行應用程序
CMD ["bun", "index.ts"]

NOTE

確保啟動命令對應於你的應用程序的入口點。如果你在 `package.json` 中有啟動腳本,這也可以是 `CMD ["bun", "run", "start"]`。

此鏡像在容器內使用 Bun 安裝依賴項並運行你的應用程序。如果你的應用程序沒有依賴項,你可以省略 RUN bun install --production --frozen-lockfile 行。

在項目根目錄創建一個新的 .dockerignore 文件。此文件包含應從容器鏡像中排除的文件和目錄,例如 node_modules。這會使你的構建更快、更小:

docker
node_modules
Dockerfile*
.dockerignore
.git
.gitignore
README.md
LICENSE
.vscode
.env
# 任何其他你想要排除的文件或目錄

部署你的服務

確保你在包含 Dockerfile 的目錄中,然後直接從本地源代碼部署:

NOTE

將 `--region` 標志更新為你首選的區域。你也可以省略此標志以獲取交互式提示來選擇區域。
bash
gcloud run deploy my-bun-app --source . --region=us-west1 --allow-unauthenticated
txt
Deploying from source requires an Artifact Registry Docker repository to store built containers. A repository named
[cloud-run-source-deploy] in region [us-west1] will be created.

Do you want to continue (Y/n)? Y

Building using Dockerfile and deploying container to Cloud Run service [my-bun-app] in project [my-bun-app-...] region [us-west1]
✓ Building and deploying... Done.
  ✓ Validating Service...
  ✓ Uploading sources...
  ✓ Building Container... Logs are available at [https://console.cloud.google.com/cloud-build/builds...].
  ✓ Creating Revision...
  ✓ Routing traffic...
  ✓ Setting IAM Policy...
Done.
Service [my-bun-app] revision [my-bun-app-...] has been deployed and is serving 100 percent of traffic.
Service URL: https://my-bun-app-....us-west1.run.app

訪問你的實時應用程序

🎉 你的 Bun 應用程序現已上線!

訪問服務 URL(https://my-bun-app-....us-west1.run.app)以確認一切正常運行。

Bun學習網由www.bunjs.com.cn整理維護