How to Deploy a Docker Container Image on Google Cloud Run Using Google Cloud Build

Safe
HostSpace Cloud Solutions
3 min readJan 12, 2024

This is my first week as a DevOps Intern at HostSpace Cloud Solutions and my initial task involved deploying a Docker Container Image on Google Cloud Run using Google Cloud Build.
Coming from an AWS background, I encountered my first challenge with the comparatively stringent billing rules of GCP. It took a bit of trial and error with different cards before successfully creating my Google Cloud account.

Following that, I set up my first GCP project and activated essential resources like Cloud Build, Cloud Run, and Cloud Shell Editor, among others. These resources can be easily located by typing their names in the search bar above your dashboard.

My mentors generously provided a Docker image, and my primary objective was to deploy it. I tackled this task using two different methods.

The first method involved running a command in the Cloud Shell Editor:

gcloud run deploy my-cloudrun-service — image=us-docker.pkg.dev/cloudrun/container/hello — platform managed — project=[YOUR-PROJECT-ID]

As the Dockerfile already existed, this command deployed the specified Docker image to Google Cloud Run, creating a new Cloud Run service named “my-cloudrun-service.” The command prompted for necessary information, and upon completion, Google Cloud Run provided a URL to access the deployed application.

For the second method,

I created a `cloudbuild.yaml` file with the arguments “pull, tag, push, run/deploy” to orchestrate the build and deployment processes. After creating the file, I manually triggered the build process via the Google Cloud Console. Initially using the “global” region.
The first 8 attempts failed at the deployment stage with an error stating, ERROR: (gcloud.run.deploy) You must specify a region. Either use the ` — region` flag or set the run/region property.
I resolved this by adding the region property to my `cloudbuild.yaml` file.

The second error I encountered was
PERMISSION_DENIED: Permission ‘run.services.get’ denied on resource ‘namespaces/[Project ID]/services/my-cloudrun-service’.
After multiple trials and research, I addressed this by manually adding the option:

options:
logging: CLOUD_LOGGING_ONLY

to my `cloudbuild.yaml` file and creating a Cloud Run service in the same region as specified in my `cloudbuild.yaml` file.

The third error stated,
Your build failed to run: generic::invalid_argument: if ‘build.service_account’ is specified, the build must either (a) specify ‘build.logs_bucket’, (b) use the REGIONAL_USER_OWNED_BUCKET build.options.default_logs_bucket_behavior option, or © use either CLOUD_LOGGING_ONLY / NONE logging options.
I resolved this by updating the permissions for my service account.

Deployed Image

Upon completing the build and deployment process, I accessed the deployed application through the Cloud Run service URL provided in the Google Cloud Console.

For further details, refer to the documentation at [https://cloud.google.com/build/docs/deploy-containerized-application-cloud-run](https://cloud.google.com/build/docs/deploy-containerized-application-cloud-run).

Note: Understanding your specific errors based on the structure and arguments of your `cloudbuild.yaml` file is crucial.

With this, my final words to you are:
Congratulations!🥂 You have successfully deployed a container image to Cloud Run.🚀

--

--

Safe
HostSpace Cloud Solutions

An experienced web developer and DevOps engineer learning and experimenting with new technologies, sharing knowledge and helping beginners in the tech community