recipes/cloud-run

Cloud Run

Deploying to Google Cloud Run.

Cloud Run Example

Github StackBlitz

This example demonstrates how to deploy a Go Fiber application to Google Cloud Run.

Description

This project provides a starting point for deploying a Go Fiber application to Google Cloud Run. It includes necessary configuration files and scripts to build and deploy the application using Docker and Google Cloud Build.

Requirements

GCP Prerequisites

Before deploying, ensure the following GCP APIs are enabled and IAM roles are granted:

Enable APIs:

gcloud services enable run.googleapis.com \
  cloudbuild.googleapis.com \
  containerregistry.googleapis.com

IAM roles required for the Cloud Build service account ([PROJECT_NUMBER]@cloudbuild.gserviceaccount.com):

  • roles/run.admin β€” deploy Cloud Run services
  • roles/iam.serviceAccountUser β€” act as the Cloud Run runtime service account
  • roles/storage.admin β€” push images to Container Registry

Setup

  1. Clone the repository:

    git clone https://github.com/gofiber/recipes.git
    cd recipes/cloud-run
  2. Install the dependencies:

    go mod download
  3. Build the Docker image:

    docker build -t cloud-run-example .
  4. Run the Docker container:

    docker run -p 3000:3000 cloud-run-example

The application should now be running on http://localhost:3000.

Architecture: Cloud Run runs linux/amd64 images. gcloud builds submit (below) builds one on Cloud Build, and so does docker build on an amd64 machine. On an arm64 machine, such as an Apple silicon laptop, build the image you push with docker build --platform linux/amd64 -t cloud-run-example .. The Dockerfile compiles for the platform the image is built for: BuildKit passes it in as TARGETOS and TARGETARCH, and a builder that does not, such as the legacy Docker builder, builds for the platform it runs on. On Cloud Build, with the Docker builder or with the Kaniko step of cloudbuild.yaml, that is amd64. With --platform linux/amd64 on an arm64 machine the whole build runs in amd64 images, so Docker's emulation has to be available (Docker Desktop includes it).

PORT environment variable: Cloud Run injects the PORT env variable at runtime. The application reads PORT and falls back to 3000 if unset, so it works both locally and on Cloud Run without any code changes.

Deploy to Google Cloud Run

  1. Set up Google Cloud SDK and authenticate:

    gcloud auth login
    gcloud config set project [YOUR_PROJECT_ID]
  2. Build and push the Docker image using Google Cloud Build:

    gcloud builds submit --tag gcr.io/[YOUR_PROJECT_ID]/cloud-run-example
  3. Deploy the image to Cloud Run:

    gcloud run deploy cloud-run-example --image gcr.io/[YOUR_PROJECT_ID]/cloud-run-example --platform managed --region [YOUR_REGION] --allow-unauthenticated

    Note: --allow-unauthenticated makes the service publicly accessible. Remove this flag in production and use IAM-based access control instead.

Replace [YOUR_PROJECT_ID] and [YOUR_REGION] with your Google Cloud project ID and desired region.

Cloud Build Configuration

The cloudbuild.yaml file defines the steps to build and deploy the application using Google Cloud Build:

steps:
  - name: 'gcr.io/kaniko-project/executor:latest'
    id: 'build-and-push'
    args:
      - '--destination=asia.gcr.io/$PROJECT_ID/$_SERVICE_NAME:$SHORT_SHA'
      - '--destination=asia.gcr.io/$PROJECT_ID/$_SERVICE_NAME:latest'
      - '--dockerfile=Dockerfile'
      - '--context=.'
      - '--cache=true'
      - '--cache-ttl=120h'

  - id: 'Deploy to Cloud Run'
    name: 'gcr.io/cloud-builders/gcloud'
    entrypoint: 'bash'
    args:
      - '-c'
      - |
        gcloud run deploy $_SERVICE_NAME \
        --image=asia.gcr.io/$PROJECT_ID/$_SERVICE_NAME:$SHORT_SHA \
        --region=$_REGION --platform managed \
        --allow-unauthenticated \
        --port=3000
        # NOTE: --allow-unauthenticated is for demo purposes only.
        # Remove this flag in production and use IAM-based access control instead.
options:
  substitutionOption: ALLOW_LOOSE

substitutions:
  _SERVICE_NAME: cloud-run-example
  _REGION: asia-southeast1

Example Usage

  1. Open your browser and navigate to the Cloud Run service URL provided after deployment.

  2. You should see the message: Hello, World!.

Conclusion

This example provides a basic setup for deploying a Go Fiber application to Google Cloud Run. It can be extended and customized further to fit the needs of more complex applications.

References