How to Deploy Your Website on Cloud Run ?

Biswanath Giri
4 min readSep 21, 2022

--

Architecture diagram

The exercises are ordered to reflect a common cloud developer experience:

  1. Create a Docker container from your application
  2. Deploy the container to Cloud Run
  3. Modify the website
  4. Roll out a new version with zero downtime

How to build a Docker image using Cloud Build and upload it to Artifact Registry

Clone the source repository

git clone https://github.com/googlecodelabs/monolith-to-microservices.git
cd ~/monolith-to-microservices

Run the following command to install the NodeJS dependencies so you can test the application before deploying

./setup.sh

Create the target Docker repository

  1. From the Navigation Menu, under CI/CD navigate to Artifact Registry > Repositories.
  2. Click Create Repository.
  3. Specify monolith-demo as the repository name.
  4. Choose Docker as the format.
  5. Under Location Type, select Region and then choose the location us-central1.
  6. Click Create.

Configure authentication

gcloud auth configure-docker us-central1-docker.pkg.dev

Deploy the image

First you need to enable the Cloud Build, Artifact Registry, and Cloud Run APIs. Run the following command to enable

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

After the APIs are enabled, run the following command to start the build process

gcloud builds submit — tag us-central1-docker.pkg.dev/${GOOGLE_CLOUD_PROJECT}/monolith-demo/monolith:1.0.0

To view your build history, or watch the process in real time, in the Console, from the Navigation menu click Cloud Build > History. Here you can see a list of all your builds; there should only be 1 that you just created.

If you click on the Build ID, you can see all the details for that build including the log output.

From the Build Details page you can view the container image that was created by clicking the Execution Details tab, then clicking on on the image link.

How to deploy Docker images to Cloud Run?

Deploy the container to Cloud Run

gcloud run deploy monolith — image us-central1-docker.pkg.dev/${GOOGLE_CLOUD_PROJECT}/monolith-demo/monolith:1.0.0 — region us-central1

Create new revision

gcloud run deploy monolith — image us-central1-docker.pkg.dev/${GOOGLE_CLOUD_PROJECT}/monolith-demo/monolith:1.0.0 — region us-central1 — concurrency 1

On the Service Details page, click on the Revisions tab. You should now see 2 revisions created.

The most recent deployment has Details on the right hand side.

You will see that the concurrency value has been reduced to “1”.

  1. Run the following command to update the current revision, using a concurrency value of 80:

gcloud run deploy monolith — image us-central1-docker.pkg.dev/${GOOGLE_CLOUD_PROJECT}/monolith-demo/monolith:1.0.0 — region us-central1 — concurrency 80

Make changes to the website

Run the following commands to copy the updated file to the correct file name.

cd ~/monolith-to-microservices/react-app/src/pages/Home
mv index.js.new index.js

Print its contents to verify the changes:

cat ~/monolith-to-microservices/react-app/src/pages/Home/index.js

Run the following command to build the React app and copy it into the monolith public directory:

cd ~/monolith-to-microservices/react-app
npm run build:monolith

Run the following command to trigger a new Cloud Build with an updated image version of 2.0.0

cd ~/monolith-to-microservices/monolith
gcloud builds submit — tag us-central1-docker.pkg.dev/${GOOGLE_CLOUD_PROJECT}/monolith-demo/monolith:2.0.0

Update website with zero downtime

Run the following command to re-deploy the service to update the image to a new version with the following command

gcloud run deploy monolith — image us-central1-docker.pkg.dev/${GOOGLE_CLOUD_PROJECT}/monolith-demo/monolith:2.0.0 — region us-central1

Verify deployment

gcloud run services describe monolith — platform managed — region us-central1

--

--

Biswanath Giri
Biswanath Giri

Written by Biswanath Giri

Cloud & AI Architect | Empowering People in Cloud Computing, Google Cloud AI/ML, and Google Workspace | Enabling Businesses on Their Cloud Journey

No responses yet