Hosting a website on Google Cloud using Cloud Run

Get Cooking in Cloud

Priyanka Vergadia
Google Cloud - Community

--

Introduction

In this mini series we are covering, how to create websites on Google Cloud. This is the third article in the series.

  1. Hosting web apps on Google Cloud: An Overview
  2. Hosting a web app on Google Cloud using Google Cloud Storage
  3. Hosting a web app on Google Cloud using Cloud Run (This blog)
  4. Hosting a web app on Google Cloud using Google Compute Engine
  5. Scaling web app on Google Compute Engine.
  6. Case Study

Let’s say you’re a small company that got really big very quickly, and now you are seeing a lot of traffic on your simple website, but you don’t want to deal with infrastructure setup and maintenance when you could be focusing on your core business!

What you’ll learn

  • Create a website using Cloud Run in five simple steps.

Prerequisites

  • Read the first article that covers high level concepts about setting up websites on Google Cloud.

Check out the video

What is Cloud Run?

Cloud Run is a managed compute platform that automatically scales stateless containers. “Stateless” is important here, since container instances can be started or stopped at any time. These containers can be invoked by web requests or pub/sub events.

It is built from Knative, letting you choose to easily run your containers either fully managed with Cloud Run, or in your Google Kubernetes Engine cluster with Cloud Run on GKE.

Cloud Run is also serverless: it abstracts away all infrastructure management, so you can focus on what matters most — building great applications.

How to create website on Cloud Run

Before we begin, we need to make sure we are properly set up in Google Cloud Console. Here are some setup logistics:

  • You have a GCP project created
  • A Billing account is set up
  • Enable the cloud run and cloud build APIs.
  • If you don’t have the Google Cloud SDK installed, go ahead and install it following the instructions here.

Now that we’ve covered the logistics, there are five steps to set up a web app on Cloud Run.

Step 1 : Install beta components of the gcloud SDK

NOTE: At the time of this blog post’s publish date, Cloud Run is in Beta.

This requires us to install the beta components of the gcloud SDK using the following commands.

“gcloud components install beta” — to install the beta features

“gcloud components update” — to update the components

Step 2 : Create a sample web application to deploy

You could skip this step if you already have a web app that you would like to use further in these steps. If you choose to follow along with my example, then let’s create a “hello world” web app in python using the following steps:

  • “mkdir helloworld-python” — create a directory
  • “cd helloworld-python” — change to the directory created
  • “vi app.py” — create a file and paste the following code. This creates a basic web server that listens on the port defined by the PORT environment variable.
import os

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
target = os.environ.get('TARGET', 'World')
return 'Hello {}!\n'.format(target)

if __name__ == "__main__":
app.run(debug=True,host='0.0.0.0',port=int(os.environ.get('PORT', 8080)))

Now, we are ready to containerize our app and upload to container registry!

Step 3 : Containerize the app and upload to Container Registry

Create a new file named Dockerfile in the same directory as the source files. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble a container image.

you could use the following commands:

  • “pwd” — to check which directory you are in. If you are not in the “helloworld-python” directory, then change to it.
  • “cd helloworld-python” — change to the directory created
  • “vi Dockerfile” — command to create the file in the same directory. Paste the code below in this file to create the container image.
# Use the official Python image.
# https://hub.docker.com/_/python
FROM python:3.7

# Copy local code to the container image.
ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . .

# Install production dependencies.
RUN pip install Flask gunicorn

# Run the web service on container startup. Here we use the gunicorn
# webserver, with one worker process and 8 threads.
# For environments with multiple CPU cores, increase the number of workers
# to be equal to the cores available.
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 app:app

Step 4 : Build

Now, we are ready to build using cloud build by running the build command from the directory containing the Dockerfile.

  • “gcloud builds submit — tag gcr.io/gcic-web/helloworld”- this will submit the build. You will see “success” when the build is successful.

Step 5: Deploy on Cloud Run

To deploy the container image on Cloud Run, we now need to execute the following command. Make sure you replace the PROJECT-ID and SERVICE-NAME with your own project-id and service name.

  • “gcloud beta run deploy — image gcr.io/PROJECT-ID/SERVICE-NAME — platform managed”
  • When prompted to select the region, choose the region of your choice.
  • Provide a service name of your choice
  • Respond with a Yes to allow unauthenticated invocations.
  • On success, we see the service URL. You can use the URL to open the web app we just deployed.

Congratulations!!

You have just deployed a web application packed in a container image on Cloud Run.

Conclusion

We created and deployed a dynamic web application on Cloud Run and learned that if you host a website on Cloud Run, you can rely on google cloud for it to scale with the demand.

Web application deployed on Cloud Run

Next steps

--

--

Priyanka Vergadia
Google Cloud - Community

Developer Advocate @Google, Artist & Traveler! Twitter @pvergadia