Streamlit Deployment on Google Cloud Serverless Container Platform

Jishnu
dSights Write
Published in
6 min readJul 28, 2021

A complete recipe with code, detail steps and explanations.

Streamlit (https://streamlit.io/) is becoming very popular among data scientists and also an effective open-source tool for making data apps for various applications. The overall usefulness of these apps depends on the ease of hosting. A significant proportion of these apps are used for limited period experiments, proof-of-concept demos, or solutions with minimal or intermittent usage in many situations. Good economics suggests that an option for pay-as-you-go / serverless deployment would be a good option.

Enthusiasts and community contributors have experimented with the deployment solutions in various cloud platforms — Heroku, AWS EC2, AWS Fargate, GCP App Engine, GCP GKE, and other clouds. These deployments, except GCP App Engine, are not truly serverless. In simple terms, these options do not follow the pay-as-you-go pricing model. We have explored the possibility of deploying Streamlit App in GCP Cloud Run — a highly scalable, fully managed serverless platform for deploying containerized applications.

An example of successful deployment on GCP Cloud Run is shown below:

The app URL is :

https://strealit-demo-r6nrud3izq-el.a.run.app

In next few sections, we outline the steps of cloud run deployment. GitHub link will provide the code and instructions.

Streamlit Deployment on Google Cloud Run Platform is broken into five steps, explained below:

Step 1: Complete the App Development.

A repo with app.py, related modules and requirements.txt, and associated files. The image below is a snapshot of the completed app, tested with streamlit run app.py

Step 2: Creation of docker image of the Streamlit App

Docker Image creation needs the execution of the following steps:

  1. Dockerfile Creation
  2. Docker build to build the docker image

A minimal example of the dockerfile is shown below:

FROM python:3.7.2

RUN pip install --upgrade pip

WORKDIR /app

COPY requirements.txt .

RUN pip install --no-cache-dir -r requirements.txt


ENV PORT=


COPY app.py .
COPY utils.py .
COPY data ./data/

CMD streamlit run app.py --server.port=${PORT}
--browser.serverAddress="0.0.0.0"

The key points of the Dockerfile design are as follows:

  1. Dockerfile / Image should be tweaked to accommodate host and port values to follow the GCP Cloud Run specification.
  2. GCP Cloud Run Application should have host IP = “0.0.0.0”, which can be accomplished by modifying the “browser.serverAddress” configuration of the Streamlit settings.
  3. App should be able to run on the port supplied by the host, and this port could be dynamic based on the actual deployment of the instance.

Line 12 (ENV Statment) and 19 (CMD Statement) of the dockerfile helps to configure the port as per cloud run specifications.

The docker image for Streamlit App, could be created running the following command:

docker build -t st_demo:v1.0 .

Step 3: Local testing of docker image

The local testing of the image created as above could be accomplished as per below in Linux Machine.

  1. Open a terminal
  2. Create a SHELL variable MYPORT, with values set to 9090.
  3. Run the docker image as per the configuration below:
docker run --rm --name mytest -p 8900:${MYPORT} -e PORT=${MYPORT} st_demo:v1.0

The above could be explained as below:

Run a docker container using the image st_demo:v1.0 with host port =8900 and container port mapped to the value of the variable MYPORT, which has been set with a value 9090.

If this statement is new to you, you may consult the link below:

Once all of that is complete , in the browser type “0.0.0.0:8900”, the app should open in the browser, an example is shown below:

Step 4: Pushing the local image to Google Container Repo

The local docker image needs to be pushed into GCP Container Registry to prepare for deployment on Google Cloud Run.

The local docker image needs to be pushed into GCP Container Registry to prepare for deployment on Google Cloud Run. This step requires fiddling with few GCP settings, and we have used the gcloud command tool to accomplish the same; commands are listed sequentially to be executed for successful deployment.

i. gcloud init : Will initialize the GCP, you need an account, a project with billing enabled to complete this step.

ii. gcloud config set compute/region asia-south1 : Set Region

iii.gcloud config set compute/zone asia-south1-b : Set Zone

iv. gcloud services enable run.googleapis.com containerregistry.googleapis.com : Enable relevant APIs in Google Cloud.

v.gcloud auth configure-docker : Enable local docker engine to communicate with glcoud cli.

vi. docker tag st_demo:v1.0 gcr.io/< YOUR GCP PROJECT ID > /st_demo:v1.0 : Create another tag for local image to follow the gcp repo naming convention.

vii. docker push gcr.io/< YOUR GCP PROJECT ID > /st_demo:v1.0 : Push the local image to GCP Repo.

Step 5: Deploy the image into GCP Cloud Run

The final step of the process is to deploy the GCP docker image on cloud run. The deployment is easy, single line to deploy the app !

gcloud run deploy < service name >  \
--image < gcp image name> \
--platform managed
--allow-unauthenticated
--region < your region >
--memory 2Gi --timeout=900

We are passing many parameters to configure the final deployment, a short note on the parameters for your reference:

i. < service name > : User Supllied — a name for THIS deployment or service.

ii.< gcp image name> : Image Pushed into GCP as per step 4 above.

iii.< your region >: Region was set at the gcloud init step.

iv. < platform managed >: GCP Specific Parameter, a tag for cloud run specific service or product , consult GCP Manual for further details.

v. < allow-unauthenticated >: GCP Specific Parameter, this is to make service avaible to the internet,for other options consult GCP Manual for further details.

vi.< memory > : Memory to be allocated to the deployed container.

vii.- < timeout >: GCP Specific Parameter. For streamlit deployment, this value should be set to a high value to avoid a timeout / connection error.

All Done! Within 2 -3 minutes gcloud will print the URL of the app, an example below:

The app can be accessed at the following URL:

https://strealit-demo-r6nrud3izq-el.a.run.app

A file named step_by_step_guide.md at GitHub Repo for this article is a crisp reference for the process if you find this article a little too verbose.

Finally, some notes and comments for the people who are already ahead of the curve exploring streamlit deployment on various platforms:

  • As of June 2021, GCP Cloud Run support for Websockets are at general availability. A time for re-look at the feasibility of deploying Streamlit on GCP Cloud Run despite previous failures.
  • GCP IAM can be configured to eliminate the need for **credential manager**. For example, the app used in this article accesses files from GCP storage without passing service account details as environment variables to the container.

Try it out and see if it is another stable option for Streamlit Hosting.

I am excited to see whether this has unlocked another option for Streamlit Community.

✌️ ✌️ ✌️✌️✌️✌️

--

--

Jishnu
dSights Write

Data Scientist, read, write and speak about anything remotely associated with data. | www.linkedin.com/in/bbjishnu |www.dsightsonline.com