Cloud Run
Cloud Run Example
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
- Go 1.27 or higher
- Docker
- Google Cloud SDK
- Git
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.comIAM roles required for the Cloud Build service account ([PROJECT_NUMBER]@cloudbuild.gserviceaccount.com):
roles/run.adminβ deploy Cloud Run servicesroles/iam.serviceAccountUserβ act as the Cloud Run runtime service accountroles/storage.adminβ push images to Container Registry
Setup
Clone the repository:
git clone https://github.com/gofiber/recipes.git cd recipes/cloud-runInstall the dependencies:
go mod downloadBuild the Docker image:
docker build -t cloud-run-example .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/amd64images.gcloud builds submit(below) builds one on Cloud Build, and so doesdocker buildon anamd64machine. On anarm64machine, such as an Apple silicon laptop, build the image you push withdocker build --platform linux/amd64 -t cloud-run-example .. The Dockerfile compiles for the platform the image is built for: BuildKit passes it in asTARGETOSandTARGETARCH, 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 ofcloudbuild.yaml, that isamd64. With--platform linux/amd64on anarm64machine the whole build runs inamd64images, so Docker's emulation has to be available (Docker Desktop includes it).
PORT environment variable: Cloud Run injects the
PORTenv variable at runtime. The application readsPORTand falls back to3000if unset, so it works both locally and on Cloud Run without any code changes.
Deploy to Google Cloud Run
Set up Google Cloud SDK and authenticate:
gcloud auth login gcloud config set project [YOUR_PROJECT_ID]Build and push the Docker image using Google Cloud Build:
gcloud builds submit --tag gcr.io/[YOUR_PROJECT_ID]/cloud-run-exampleDeploy 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-unauthenticatedNote:
--allow-unauthenticatedmakes 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-southeast1Example Usage
Open your browser and navigate to the Cloud Run service URL provided after deployment.
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.