How to Deploy Streamlit Apps to Google Cloud Platform(GCP App Engine)

In the previous tutorial we saw how to deploy streamlit using docker. In this tutorial we will be looking at how to deploy our streamlit app to Google Cloud Platform App Engine using docker.

Docker gives you the possibility to deploy apps on any available cloud platform such as GCP,Azure,AWS ,Alibabacloud etc.

The main requirements for deploying our app on GCP  after creating an account includes.

  • Dockerfile
  • App.yaml File
  • Google Cloud SDK

Let us start

Creating the Dockerfile

For us to build a docker image and a container to run our app we will be using a Dockerfile.

A Dockerfile is a file that contains the instructions and steps for build and setting up our image and container in which our app will run . It must be named as Dockerfile without any extension.

Below is the Dockerfile we will be using.

FROM python:3.7
WORKDIR /app
COPY requirements.txt ./requirements.txt
RUN pip3 install -r requirements.txt
EXPOSE 8080
COPY . /app
CMD streamlit run --server.port 8080 --server.enableCORS false app.py

The most important step to notice for streamlit app is the port we are exposing (8080 instead of the normal default port of 8501).

Then we need to disable CORS (Cross Origin Resource Sharing) or set to false.

Creating our App.yaml file

The app.yaml file contains the configurations for our app. It includes the runtime and the environment type as well as several other instructions. In our case we will only need a custom runtime and a flexible environment.

runtime: custom
env: flex

 

Installing GCP Cloud SDK

In case you do not have a GCP account you can create one and use the free 300$ credit to try it out.

 

Then you can either create a project from the web site or from the CLI.

 

GCP has a simple CLI tool for interacting with our cloud platform for every major operating system. You can install it from here.

Setting Up Your Gcloud SDK

To check for your project from your terminal with gcloud you can use

gcloud projects list

To change to the project you created you can use

gcloud config set project your_projectname

To check for the current project you use

gcloud config get-value project

 

Deploying Our App

To deploy our app we will be using

gcloud app deploy

It will ask you same basic instructions and you can select the ones you want respectively.

It will then build an image using the Dockerfile just as it would have been in case you were doing it locally.

After sometime you will see the url (mostly your project ID + appspot.com) where you can see your app being hosted.

To open your app from the command line/terminal you can run

gcloud app browser

Deploying your app on GCP is quite easy especially using a Dockerfile and the Gcloud sdk.

Below is a video tutorial of the entire process.

Thanks for your time

Jesus Saves

By Jesse E. Agbe(JCharis)

6 thoughts on “How to Deploy Streamlit Apps to Google Cloud Platform(GCP App Engine)”

    1. jesse_jcharis

      Hi Maurizio, please was it able to work locally?
      If it did, can you check if the port is available ,and Dockerfile EXPOSE section.

      Pls let me know if it works. Thanks

      1. Yes it works locally and I deployed on GCP CloudEngine following your steps but no luck. I ended up deploying it on GKE, it’s a bit more complicated but it worked at the first attempt.

Leave a Comment

Your email address will not be published. Required fields are marked *