RuntimePackage ManagerBundlerTest RunnerLinterFormatterGuidesReferenceBlogFeedback

Deploy a Bun application on Google Cloud Run

Google Cloud Run is a managed platform for deploying and scaling serverless applications. Google handles the infrastructure for you.

This guide deploys a Bun HTTP server to Google Cloud Run using a Dockerfile.


  1. Initialize gcloud by selecting or creating a project

    Make sure that you've initialized the Google Cloud CLI. gcloud init logs you in and prompts you to either select an existing project or create a new one.

    For more help with the Google Cloud CLI, see the official documentation.

    terminal
    gcloud init
    
    Text
    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!
    
  2. (Optional) Store your project info in environment variables

    Set variables for your project ID and number so you can reuse them in the following steps.

    terminal
    PROJECT_ID=$(gcloud projects list --format='value(projectId)' --filter='projectId=my-bun-app')
    PROJECT_NUMBER=$(gcloud projects list --format='value(projectNumber)' --filter='projectId=my-bun-app')
    
    echo $PROJECT_ID $PROJECT_NUMBER
    
    Text
    my-bun-app [PROJECT_NUMBER]
    
  3. Link a billing account

    List your available billing accounts and link one to your project:

    terminal
    gcloud billing accounts list
    
    Text
    ACCOUNT_ID            NAME                OPEN  MASTER_ACCOUNT_ID
    [BILLING_ACCOUNT_ID]  My Billing Account  True
    

    Link your billing account to your project. Replace [BILLING_ACCOUNT_ID] with the ID of your billing account.

    terminal
    gcloud billing projects link $PROJECT_ID --billing-account=[BILLING_ACCOUNT_ID]
    
    Text
    billingAccountName: billingAccounts/[BILLING_ACCOUNT_ID]
    billingEnabled: true
    name: projects/my-bun-app/billingInfo
    projectId: my-bun-app
    
  4. Enable APIs and configure IAM roles

    Activate the necessary services and grant Cloud Build permissions:

    terminal
    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
    
  5. Add a Dockerfile

    Create a new Dockerfile in the root of your project. This file contains the instructions to initialize the container, copy your local project files into it, install dependencies, and start the application.

    Dockerfile
    # Use the official Bun image to run the application
    FROM oven/bun:latest
    
    # Copy the package.json and bun.lock into the container
    COPY package.json bun.lock ./
    
    # Install the dependencies
    RUN bun install --production --frozen-lockfile
    
    # Copy the rest of the application into the container
    COPY . .
    
    # Run the application
    CMD ["bun", "index.ts"]
    

    Create a new .dockerignore file in the root of your project. It lists the files and directories to exclude from the container image, such as node_modules. Excluding them keeps builds faster and smaller:

    .dockerignore
    node_modules
    Dockerfile*
    .dockerignore
    .git
    .gitignore
    README.md
    LICENSE
    .vscode
    .env
    # Any other files or directories you want to exclude
    
  6. Deploy your service

    Make sure you're in the directory containing your Dockerfile, then deploy directly from your local source:

    terminal
    gcloud run deploy my-bun-app --source . --region=us-west1 --allow-unauthenticated
    
    Text
    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 configuration...
      ✓ 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
    
  7. Visit your live application

    Your Bun application is now live.

    Visit the Service URL (https://my-bun-app-....us-west1.run.app) to confirm everything works as expected.