Skip to main content

How to Deploy to Google Cloud Run (GCP, Docker)

In this article, I will show you how to create, deploy and run a simple Web app service using Google Cloud Run (or just Cloud Run for short).

info

These instructions were written and tested on a Mac.

Step 1. Clone the demo repo

This article focuses on hosting a Web app, not designing one. Below are instructions on how to clone a simple Web server that is bundled with a Dockerfile.

To get started, open up a Terminal window and clone my demo app which can be found here:

To clone the app to your machine, do the following:

mkdir -p ~/projects/cloud-run
cd ~/projects/cloud-run
git clone https://github.com/mitchallen/cloud-run-103.git

Once the repo is cloned, cd to that folder.

cd cloud-run-103

Look over the contents of the repo to get an idea of what the app and the Dockerfile are doing.

Step 2. Create a Google Cloud account

Cloud Run is part of the Google Cloud Platform (GCP).  To use it, you will need to create an account by signing up here:

A note about billing:

This article shows how to use services that Google will bill you for. New users are given credit and some services are offered for free if the usage stays below a minimal use.

The home page should mention free credits, etc. that you can read more about here (subject to change at their discretion):

Be sure to monitor billing and delete test resources when no longer needed.

See: https://console.cloud.google.com/billing

To monitor project usage, I bookmark this page:

Step 3. Create a new project

Before you can do anything, you need to create a Google Cloud Platform project.

The platform will generate a Project ID, based on the name.

  • Note the Project ID - you will need it for some command-line operations

Note that this step is specific to a Mac. It may need to be adapted for other platforms.

  • Open up a Terminal window
  • Run this command:
brew install --cask google-cloud-sdk

That should install a new command line tool called gcloud.

Other ways to install

If you have issues with the installation or need instructions for another platform, see here:

Step 5. Authorize

To authorize the gcloud CLI to work on your behalf, run this command:

gcloud auth login

This will open up a browser where you can log in and grant permission.

Step 6. Initialize a project

Once you are authenticated, you can initialize a new project.

To initialize a new project, run this command:

gcloud init

Create a config

You should be prompted with something like this:

Pick configuration to use:
[1] Re-initialize this configuration [default] with new settings
[2] Create a new configuration
  • Select option 2
  • Name it something like "demo-config"

Choose an account

You should then be prompted with something like this:

Choose the account you would like to use to perform operations for this 
configuration:
[1] (your email address)
[2] Log in with a new account
  • Pick option 1 - which will be the email address that you authenticated with

Choose a project id

Then you should be prompted with something like this:

Pick cloud project to use: 
[1] (Project ID fof last project that you selected)
[2] Enter a project ID
[3] Create a new project
  • Pick option 1 - which should be the last project that you selected in the console

Step 7. Update the components

If you just installed gcloud, this might not be necessary.  

But it won't hurt to run this command to update the components:

gcloud components update

Step 8. Enable some services

Run this command to enable some services that you will need:

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

That will enable the following services:

  • Artifact Registry - to store Docker image
  • Cloud Build - to build your Docker image
  • Cloud Run - to run your service in the cloud

Step 9. Update the region and location

Cloud companies host their servers all over the world.  So you need to select a region that you think might be close to the majority of your core users.  Since I'm in the US, I chose us-central1.

For more information and a list of the regions available for Cloud Run, see here:

Run this command to update the region and location that is specific to your use of Cloud Run (updating for your preferred region / location):

gcloud config set run/region us-central1
gcloud config set artifacts/location us-central1

Step 10. Verify gcloud info

To verify that gcloud was configured properly, run this command:

gcloud info

You should see under Current Properties in the info that your set for the artifacts and run commands, like this:

  [artifacts]
location: [us-central1] (property file)
[run]
region: [us-central1] (property file)

You should also see your project id.  If you do not have a project id, go to your console and create one:

Another way to find a project id is to use the list command:

 gcloud projects list

Step 11. Create a docker repo

Create a repo to hold your images (this replaces the deprecated Container Registry).

gcloud artifacts repositories create demo-repo \
--repository-format=docker \
--description="Docker repo"

You can confirm that it was created, using this command:

gcloud artifacts repositories list

Authorize docker with this command (substitute YOUR_REGION, for example with us-central1):

gcloud auth configure-docker YOUR_REGION-docker.pkg.dev

You will be warned, like this:

Adding credentials for: us-central1-docker.pkg.dev
After update, the following will be written to your Docker config file located at [/Users/mitch/.docker/config.json]:
{
"credHelpers": {
"us-central1-docker.pkg.dev": "gcloud"
}
}

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

Docker configuration file updated.

Step 12. Create the image

Now that you have services enabled, a project id, a region, and a repo, you can create an image.

  • Make sure that you are in the cloned projects folder
  • The command below will be looking in the current folder for a Dockerfile

Run this command to create an image (substitute REGION (like us-central1) and  PROJECT-ID):

gcloud builds submit --tag REGION-docker.pkg.dev/PROJECT-ID/demo-repo/hello

You might see a red warning like this (you can ignore it for this demo):

npm notice created a lockfile as package-lock.json. You should commit this file.npm WARN [email protected] No repository field.

To see and manage the container image, browse to:

It may take a few minutes to see it on the page.

Step 13. Deploy to Cloud Run

Now you can deploy a service based on the image to Cloud Run.

  • Run this command and substitute REGION and PROJECT-ID with your current gcloud project id:
gcloud run deploy hello-web --image REGION-docker.pkg.dev/PROJECT-ID/demo-repo/hello

Enter Y for Yes if you see this prompt:

Allow unauthenticated invocations to [hello-web] (y/N)? Y
tip

If you are concerned about billing: no one will know the URL address if you don't publicly post it anywhere. You can immediately kill the service when done testing to avoid any unauthorized access or billing.

The command line will then give you the URL of your app, with a message like this (with your own unique URL):

Service [hello-web] revision [hello-web-####] has been 
deployed and is serving 100 percent of traffic at
https://hello-web-SOME-RANDOM-ID.a.run.app

Copy the URL and paste the address into a browser or use curl.

See the service in the console

To see and manage the service in the Google Cloud Platform console, browse to:

Step 14. Cleanup

To save money you should consider taking down private and test resources when not in use. To use them again, be sure to document how to recreate them.

Visit the following console pages to delete test resources:

Delete the service

To delete the service using the command line, run this command:

gcloud run services delete hello-web

Delete the image

To delete the image use the command line (substitute REGION and PROJECT-ID with your Google Cloud Project ID):

gcloud artifacts docker images delete REGION-docker.pkg.dev/PROJECT-ID/demo-repo/hello

Delete the repo

You can delete the repo with this command:

gcloud artifacts repositories delete demo-repo

Delete the project

You can also delete the project with this command (substitute PROJECT-ID with your Google Cloud Project ID):

gcloud projects delete PROJECT-ID

You will see instructions on how to undelete the project.  You should also get an email that you have about a month to change your mind.

Delete the configuration

You can delete the configuration from the command line.

First list the available configurations:

gcloud config configurations list
tip

This is not to be confused with gcloud config list which just lists the current config.

You can't delete a configuration that is active.  So first you must switch to the default by running this command:

gcloud config configurations activate default

Now you can delete your configuration with this command (assuming that you named your config demo-config):

gcloud config configurations delete demo-config

Run the  configurations list command again to confirm it's gone and the default is active:

gcloud config configurations list

Monitor billing

To monitor billing, browse to:

Demo Script

In practice, you normally wouldn't be cutting and pasting regions, etc by hand. You would use some form of build script.

I've included in the root of the demo repo a script (demo-build.sh) that will do the following:

  • build the repo, image, and service (in that order)
  • delete the service, image, and repo (in that order)

The script will NOT delete the project or configuration.  Because you might want to keep those.

Review the script and edit it for your preferred region, etc.

Conclusion

In this article, you learned how to:

  • Setup an account on the Google Cloud Platform (GCP)
  • Install and setup the gcloud CLI (command-line interface)
  • Use gcloud to initialize, setup, and deploy images to Google Cloud Run
  • Cleanup resources to avoid billing

References

  • cloud.google.com/sdk/docs/initializing - [1]
  • cloud.google.com/sdk/gcloud/reference/config/set - [2]
  • cloud.google.com/sdk/gcloud/reference/config/configurations/create - [3]
  • cloud.google.com/run/docs/tutorials/system-packages#follow-cloud-run - [4]
  • cloud.google.com/sdk/gcloud/reference/builds/submit - [5]
  • Submit a build via CLI and API - [6]
  • Listening for requests on the correct port - [7]
  • Configure containers - [8]
  • gcloud auth configure-dock - [9]
  • cloud.google.com/artifact-registry/docs - [10]
  • artifact-registry-cloud-run-demo.sh - [11]