How to deploy Streamlit applications on GCP using Cloud Run
In this tutorial I will write about deploying a Streamlit application on Google Cloud using Cloud Run. All this will be done via the UI .
Cloud Run is a serverless product of Google which enables anyone to quickly deploy apps without any knowledge about infrastructure.
To follow this tutorial, you will need a basic understanding of Docker and how to build a Docker image. If you do not have a Docker image yet, use this example repository.
You will also need a GCP project with billing enabled and have the gcloud SDK.
Pushing your Docker image to GCR
In your Streamlit project you should have an Dockerfile. Open this file and make sure that you will remember the PORT you will expose the application to. In this example we will use port 8000, so EXPOSE 8000
.
Once you have built your Streamlit application using Docker, we need to push it to GCR. Authetnicate using the Google Cloud Command Line tool in your terminal and run the following command: docker push gcr.io/PROJECT_NAME/streamlit-application
. Make sure to replace PROJECT_NAME with your GCP project ID.
Once your build has been pushed to Google Cloud, log in to GCP and type in Container Registry in the search bar. You should see the pushed Docker build in this overview.
Running the application on Cloud Run
Go to the search bar of the GCP console and search for “Cloud Run”. Click on “CREATE SERVICE”.
You will see the option “Deploy one revision from an existing container image”. Make this sure this is selected and click on “SELECT” to select your Docker build. Select the latest revision.
Following that, set a “Service name ” and call it “streamlit-application”. Select region “europe-west1 (Belgium)”.
To save costs, we will keep the option to only allocate CPU during request processing.
In the section “Autoscaling” we will set the minimum number of instances to 0 to save costs. This can make the application loading time a bit longer.
Open the “Advanced Settings ”section and make sure you put the Container port to 8000.
Click on Next, and select “Allow all traffic” and “Allow unauthenticated invocations”.
Access your application
After a few minutes the app should have been created and you should see a link that ends with “run.app”.
Copy this link and you should be able to visit this application.