Cloud Run as a Serverless Platform to Deploy Containers

Ajay Last Updated : 12 May, 2023
10 min read

Introduction

In recent years, deploying Containers has become a common trend in many companies. Applications are built, then turned into Images, and these Containers are run in the cloud to be visible to everyone. Containerization provides isolation of the application from the operating system. And a Containerized application can run anywhere in different operating systems and can be deployed in different cloud platforms.

Many Cloud Platforms allow developers to deploy Running Containers in their platforms. These Containers can vary from static websites to data science applications to APIs. Popular cloud platforms for these services include Google Cloud Platform, Amazon Web Services, Microsoft Azure, and many more. There are even other small cloud platforms(these do not ask for personal information), like Railway, Fly.io, and Render, which allow containerizing and deploying apps. In this article, we will look into the Google Cloud Platform, which offers a service called Cloud Run that allows developers to deploy Containers in the cloud.

Learning Objectives

  • Understand how Cloud Run works.
  • Learning how to create a service in Cloud Run.
  • Learn how to deploy Containers in Cloud Run.
  • Understanding the benefits of Cloud Run.

This article was published as a part of the Data Science Blogathon.

Table of Contents

What is Cloud Run?

Cloud Run is one of Google’s fully-managed Compute engines that allows us to deploy running Containers in the cloud, which run directly on Google’s scalable infrastructure. Cloud Run is simple to use, and we don’t have to provide an infrastructure configuration, as it is automatically taken care of, and we do not even need to manage the cluster. Cloud Run can be used to deploy different applications written in different programming languages only if they can be containerized.

Cloud Run provides an option for source-based deployment too, where we do not need to create a Container and just provide the programming language, and Cloud Run takes care of building an Image for our application. Cloud Run supports the creation of both services and jobs. The application we deploy with Cloud Run can be viewed publicly or privately only for a particular group. In this article, we will look into deploying a streamlit application; that is, we create an Image for a streamlit application, then deploy it with the Cloud Run service.

Cloud Run: Pricing Policy

Cloud Run comes with a free tier, where the first 2 million requests are free of cost and gets renewed monthly. Monthly, you get the first 180,000 vCPU seconds and the first 360,000 GiB-seconds (1 GiB Second means running 1 Gigabyte Instance per Second) for free. After that only, we get charged. These two mills. requests are sufficient enough for running small websites and small data science applications.

Post that, Cloud Run follows a pay-per-use policy. We are charged only based on the resources used by the Container, that is, the amount of CPU, Outbound Network, and memory used rounded to the nearest 100ms. The prices are split again into Tier 1 and Tier 2. Where Tier 1 contains a set of geo-locations, and Tier 2 contains another set of geo-locations. If we choose to deploy the Container at a location that is in Tier 1, then we will be charged based on Tier 1 prices. For more information on Cloud Run pricing, click here.

Creating the Streamlit Application

In this section, we will create a Streamlit application in Python, then we will build an Image out of it, and then finally, we will be deploying it in the Cloud Run. Before starting this, ensure you have an active Google Cloud Platform account. Do not worry if your free trial has ended, as the free credits for Cloud Run are applicable even after the free trial for a lifetime. The streamlit app we will create is a simple application where the user inputs a range of values for the X variable and a range of values for the Y variable, and then a plot of X vs. Y is drawn and displayed on the website. The code for creating the above-stated application can be referred to below:

import streamlit as st
import pandas as pd


# Setting up the website title and icon
st.set_page_config(page_title="Data Visualization",
                   page_icon=":chart_with_upwards_trend:")


# Title to be displayed on the website
st.title("Sample Visualization")


# User Input for X values is Taken
st.write("## X Data")
x = st.text_input(label="Provide X values with spaces").split()
x = list(map(int,x))


# User Input for Y values is Taken
st.write("## Y Data")
y = st.text_input(label="Provide Y values with spaces").split()
y = list(map(int,y))


# If X and Y values are provided, a plot is displayed
if x and y:
    data = pd.DataFrame({'x':x,'y':y})
    st.write("### X vs Y Graph")
    st.line_chart(data=data,x='x',y='y')

Let’s test our application by running it with the below command

$ streamlit run app.py

Now a window will pop up, and the website we created will appear.

"sample visualization | cloud run | google cloud platform

Now let’s input x and y values. For x, let’s give the values 1,2,3,4, and for Y, let’s give the values 1, 8, 27, 64. Now when we press enter, a graph X vs. Y will be displayed. This can be observed in the below picture.

"sample visualization
"X vs Y Graph

We have finally prepared our application. Now is the time to Containerize it, that is, create an Image out of it. For this, we need to prepare a Dockerfile, which states how to build our Image. Below is the Dockerfile created for our streamlit application.

# Setting the Base Image
FROM python:3.7.9-slim

# Copy all files from current directory to app directory
COPY . /app

# Change working directory to app directory
WORKDIR /app

# Run below command to install all the libraries
RUN pip install -r requirements.txt

# Expose port 8080
EXPOSE 8080

#Running the application
ENTRYPOINT ["streamlit", "run", "app.py", "--server.port=8080","--server.address=0.0.0.0"]

Here I’m taking the base Image as Python 3.7.9, on which our application will be built. The requirements.txt contain streamlit and pandas because we have only used these two libraries in our application. You can notice I have changed the port number to 8080. I want the application to run on port 8080. Hence I made the following changes. Also, to make our app visible to the outside of the Container, we need to expose this port. Hence the line EXPOSE 8080 is run. Then we will create the Image, and after creating the Image, we will push it to the Google Container Registry.

Enabling Google Cloud Platform Services

Before creating the Image, we must create a Project in the Google Cloud Console and enable some of its Services. In this section, we will look at those services that need to be enabled to run our applications. The first is the c API which enables us to push the image to this Registry. To create a new project, go to console.cloud.google.com, and then on the top left, you will see your current project name, My First Project is the default.

"cloud run | google cloud platform

Now click on the My First Project, and then we can create a new project.

"project selection

After creating a new project, the first thing we have to do is to enable Google Container Registry (GCR) API. GCR is a private repository maintained by google, where we can push our Images to. In GCR, we can push the images to different locations. To enable the GCR, on the search bar, type GCR and we find something called the Container Registry, which can be seen in the picture below.

"cloud run | google cloud platform

Now click on the Container Registry, and the following page will appear. Then click on the blue colored ENABLE button to enable the GCR API.

"Google container registry API| cloud run | google cloud platform

Along with enabling GCR API, we also need to enable the Cloud Run API. We can enable this by searching for Cloud Run API in the search bar and selecting the Cloud Run API. Then you will find a blue button named ENABLE, as shown in the picture; we need to click on it to enable the Cloud Run API.

"cloud run API | google cloud platform

Deploying the Streamlit Application in Cloud Run

In this section, we will move to the deployment phase and discuss how to deploy the streamlit application in the Cloud Run. After enabling the GCR API, we are ready to push to Image to our private registry. Now let’s build our image in the local environment and then push it to the GCR. While creating the Image, it’s better to follow the GCR naming convention to name Image.

gcr.io/project-id/application-name

Above is the naming convention to name the Image that we will build. For example, if our project name is cloudrun and our application name is streamlit_test, then the name of our Image will be gcr.io/cloudrun-313143/streamlit_test. Remember that the project ID is the ID assigned to our project. If our project is named cloudrun, then its ID will be something like cloudrun-313143, which is project_name-number. You need to provide the project ID while naming the Image.

If we want to push our image to GCR but want the Image to be present in the Asia region, then we can specify the name as asia.gcr.io/my-project/application-name. For Europe, it will be eu.gcr instead of asia.gcr and for the US it is us.gcr. Now let’s build our Image by using the above naming convention.

$ docker build -t asia.gcr.io/cloudrun-313143/streamlit_test .

This will build our streamlit Image with the name asia.gcr.io/cloudrun-313143/streamlit_test. Now we need to push this image to the GCR. For this, we use the gcloud command-line tool. Make sure it’s installed. Now use the command gcloud init or gcloud auth login, this will make you authenticate with the email address that we have selected to create the Google Cloud Platform account. After verification, you can set the project to the one we created for this article.

You can also set the project by using the command gcloud config set project project-id. Now we have set the project to the project we are working on. Now we need to verify the docker, so docker can push images to our private repository, which is GCR. For this, we use the command gcloud auth configure-docker. Now we push the image to the Google Cloud Repository using the command (Replace the project-id and the application name with your respective names.)

$ gcloud config set project project-id
$ gcloud auth configure-docker
$ docker push asia.gcr.io/cloudrun-313143/streamlit_test

The Image will be successfully pushed to the Container Registry. To check this, open the Container Registry in Google Cloud Platform, and we will find a folder named streamlit_test; when we click on this, we can see the Image we uploaded to Container Registry. The Image name will be some hash(numbers and alphabets). We see that the size of the Image is 219.7MB.

cloud run | container registry

Let’s move on to the final phase, which is to deploy the Image. The Container Registry will have a three-dot menu on the right side of the uploaded Image row. We will get a few options to perform when we click on it. In this, select the Deploy to Cloud Run option.

"cloud run | streamlit test

The Cloud Run Service page gets opened now. Here the Image is auto-selected. You can name the application whatever you want, here, we are naming it streamlit-test.

cloud run | google cloud platform

If you scroll down, then you will see an option called Authentication. Here we select Allow unauthenticated invocations if we want to set the streamlit website to be public so that anyone can access it. Or if we want to make it restricted to only a particular group, we can select the above option. Now click on the Create button to create the service, that is, to deploy our Image.

"container network security | google cloud platform

It will now take a while to create a Container from the Image; then, the Container will be deployed on Google’s Server. When done with deploying, it provides an URL where we can access our Streamlit Website; after completion, the page will look as follows.

"streamlit test | google cloud platform

Clicking on the URL, we can see that we can access our Streamlit Website, and the application is working fine. This link can be shared with anyone who wants to visit the website. So this is the small example we have gone through of deploying Images in the cloud with the help of the Cloud Run service.

"cloud run service | google cloud platform
cloud run service | google cloud platform

Why Should You Use Cloud Run?

Cloud Run is a fully-managed serverless platform, meaning that we only need to work on our application, and Google will take care of the servers, networking, and other things needed for it. Also, Cloud Run has an auto-scale feature, which automatically scales up when the number of users visiting our website increases and automatically scales down when the number of visiting users decreases. The scale can go down to zero.

That is, when no one visits the application, the application shuts off, thus saving the running costs drastically. Once a request comes into the deployed application, an active container will be created. Developers need to pay only the requests, that is, only if someone visits the websites, i.e., the CPU will be allocated only when a request comes in, thus saving costs by not running the application continuously even when there are no visitors.

It also supports source-based deployments where we directly provide the code and the programming language we have chosen, then the Cloud Run will automatically build the Image and then deploys it to the cloud. Cloud Run supports both services and jobs. Services can be websites that run continuously, and jobs are the code that needs to be run only once and then quit.

It supports public and private deployments, where we can deploy the website to be visible to everyone or only to a set of people. Cloud Run can seamlessly integrate with other Google Cloud Services. We can also set the Cloud Run for Continuous Delivery by uploading the code to Github, so when there is a code change, a new Image will be built, and then a new Container is created and deployed to the cloud.

Conclusion

Cloud Run is one of Google’s Compute Engine Services, which deploys containers, that is, to run the Containers in the cloud. Cloud Run is a serverless platform. Cloud Run manages all the infrastructure required to run the Container so that the developer can focus on the application end.

Some of the key features of Cloud Run include:

  1. Monthly up to 2 mills. Requests are free for Cloud Run
  2. Cloud Run auto-scales based on the website traffic and can even scale down to zero, thus saving costs
  3. Cloud Run charges only for the CPU and Memory used but not for the idle time of the application
  4. Cloud Run allows for restricted access, i.e., for the Containers to be accessed only by a set of people

The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.

I work as a Developer in the field of Data Science. I constantly spend time learning new things be it related to AI, DataSceine, and CyberSecurity. Deep learning and machine learning are two topics that I find particularly fascinating, and Python is my preferred language for programming. Cyber Security is another field that I'm touching upon recently. I have experience with large-scale data analysis, and I have a solid grasp of a variety of deep learning and machine learning approaches, including neural networks, regression models, and natural language processing. I'm eager to take on new challenges and make a meaningful contribution to the industry, so I'm constantly seeking for ways to enlarge and deepen my knowledge and skills in the subject.

Responses From Readers

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details