This Streamlit deployment is done with Google cli
Setting up google
Account point of view you need to have a valid Google Cloud account. This means that billing information needs to be done. For running commands from your local terminal install google cli
Install google sdk with brew
brew install --cask google-cloud-sdk
Login to google cli
gcloud auth login
Then create a project that you will use for the Streamlit.
gcloud projects create example-foo-bar-1 --name="Happy project" --labels=type=happy
Docker file can be found from here
This Dockerfile is used to create a Docker image for running a Streamlit web application on a Python environment. Below is an explanation of each section and command in the Dockerfile:
FROM python:3.10-slim
This line specifies the base image to use. In this case, it uses the official lightweight Python image from Docker Hub with Python version 3.10 (slim variant). This provides a minimal Python environment to run the application.
WORKDIR /streamlitgcp
This sets the working directory inside the Docker container to /streamlitgcp. Subsequent commands will be executed relative to this directory.
RUN apt-get update && apt-get install -y \
build-essential:
Essential build tools and libraries.
curl:
Command-line tool for transferring data with URLs.
software-properties-common:
Common software properties such as add-apt-repository.
git:
Version control system.
&& rm -rf /var/lib/apt/lists/*
RUN git clone https://github.com/kkaarel/streamlitgcpterraform.git .
RUN dir -s
RUN pip3 install -r ./streamlitgcp/requirements.txt \
pip install --upgrade pip
EXPOSE 8080
HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health
ENTRYPOINT ["streamlit", "run", "./streamlitgcp/app.py", "--server.port=8080", "--server.address=0.0.0.0"]
Overall, this Dockerfile sets up a Python environment, installs necessary packages, clones a Streamlit application from a GitHub repository, and configures the container to run the Streamlit app on port 8080 with external accessibility.
Building a CI/CD pipeline would be the perfect solutions. I will do a blog about how terraform can be used to deploy Streamlit and other Google Cloud components.
Deploying a Docker image to Google is a very straight forward operations.
Go to your directory that you have the docker file and run
gcloud run deploy
Logs that will come:
Deploying from source. To deploy a container use [–image]. See https://cloud.google.com/run/docs/deploying-source-code for more details.
Source code location (/Users//googlecloud/streamlitterraformdemo/streamlitgcp):
Next time, use gcloud run deploy --source .
to deploy the current directory.
Service name (streamlitgcp): streamlitgcp
Please specify a region:
….
[15] europe-west1
….
Please enter numeric choice or text value (must exactly match list item): 15
To make this the default region, run gcloud config set run/region europe-west1
.
This command is equivalent to running gcloud builds submit --tag [IMAGE] /Users/<youruser>/googlecloud/streamlitterraformdemo/streamlitgcp
and gcloud run deploy streamlitgcp --image [IMAGE]
Building using Dockerfile and deploying container to Cloud Run service [streamlitgcp] in project [] region [europe-west1]
✓ Building and deploying… Done.
✓ Uploading
sources…
✓ Building Container…
Logs are available at [https://console.cloud.google.com/cloud-build/builds/78fcf512-e717-43be-9bf5-ab56e2f6c4b5?project=591207534842].
✓ Creating
Revision…
✓ Routing
traffic…
Done.
Service [streamlitgcp] revision [streamlitgcp-00004-lk2] has been deployed and is serving 100 percent of traffic.
Service URL: https://streamlitgcp-7qkgnlop3a-ew.a.run.app