In this lab, we will deploy our guestbook application backed by Cloud Datastore to Cloud Run.
Cloud Run can run containers that are stored in public registries such as Docker Hub. However, we will use two hosted services on Google Cloud Platform that allows us to automatically build and store container images. The first service, Cloud Build, constructs container images when given an application's source files and a Dockerfile. The second service, Artifact Registry (gcr.io
), is a hosted private registry service that supports a managed equivalent to Docker Hub.
Bring up Cloud Shell and go back to the source directory for the application that was used in the initial Cloud Datastore Guestbook codelab.
cd cs430-src/05_gcp_datastore
In our initial docker container labs, we built the container via the docker CLI and pushed it to Docker Hub. In this lab, we'll build the container using Cloud Build and then push it to the Artifact Registry to host it. The command that does so is.
Create a container repository for the project
gcloud artifacts repositories create guestbook-repo \
--repository-format=docker \
--location=us-west1
When the repository has been created, list it to locate the repository URL.
gcloud artifacts repositories describe guestbook-repo \
--location=us-west1
Then, use Cloud Build to build the container
gcloud builds submit --tag \
us-west1-docker.pkg.dev/${GOOGLE_CLOUD_PROJECT}/guestbook-repo/gcp_gb
This command takes the Dockerfile and application source files in the current directory, uploads it to Cloud Build, instructs the service to build the container image, and then has the resulting container image stored in the project's private registry using the image name gcp_gb
. The container will be identical to the one built in the initial Cloud Datastore codelab.
After executing the command, from the web console, visit the Cloud Build home page, click on History, then on the build you just executed. Scroll down to see that the docker command that Cloud Build has executed on your behalf resulted in the image being created.
Visit Artifact Registry to inspect the image that has been created.
In order to deploy a container on Cloud Run, you will need to specify a service account that the container will use to access cloud resources. With the Guestbook application, this means a service account with the Cloud Datastore User
role attached. In the initial Cloud Datastore codelab, we have already created this service account.
guestbook@${GOOGLE_CLOUD_PROJECT}.iam.gserviceaccount.com
Given this service account and the container image in the previous step, we can now deploy our container serverlessly onto Cloud Run. To do so, run the following command:
gcloud run deploy gcp-gb \ --image us-west1-docker.pkg.dev/${GOOGLE_CLOUD_PROJECT}/guestbook-repo/gcp_gb \ --service-account guestbook@${GOOGLE_CLOUD_PROJECT}.iam.gserviceaccount.com
One might wonder why we do not need to set the PORT environment variable as done with prior containers. As it turns out, Cloud Run automatically sets the environment variable in order to bring the container up on the appropriate port.
When prompted, specify:
Cloud Run (fully managed)
us-west1
regionAllow unauthenticated access
Upon completion, an https URL will be returned where your container can be accessed from. Click on it or copy and paste it into a browser.
Add an entry with the message "Hello Cloud Run!". Show your Guestbook app running in a browser.
From the web console, visit Cloud Run, click on the service you created, then click on "Revisions". As the UI shows, one can create multiple revisions of a service and manage user traffic between them. This is helpful for A/B testing.
View the "Details" section to the right and answer the following questions:
Although Cloud Run has a generous free tier, it is good practice to remove resources that are not being used. Go back to the Cloud Run landing page and delete the service you've created:
Then, visit Artifact Registry and delete the container image:
Alternatively, you can also delete these resources in Cloud Shell via:
gcloud run services delete gcp-gb gcloud artifacts docker images delete \ us-west1-docker.pkg.dev/${GOOGLE_CLOUD_PROJECT}/guestbook-repo/gcp_gb