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 deployment
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 \
- This command runs updates and installs a list of essential packages using the apt-get package manager within the Docker container. The packages being installed include:
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/*
- After installing packages, this command removes cached package lists to reduce the image size. This is a common practice to keep Docker images smaller.
RUN git clone https://github.com/kkaarel/streamlitgcpterraform.git .
- This command clones a Git repository from the specified URL (https://github.com/kkaarel/streamlitgcpterraform.git) into the current directory (.) within the Docker container. It fetches the application code from the repository.
RUN dir -s
- This command lists the contents of the current directory in the Docker container. However, the -s option is not recognized by the dir command. This line may be an incorrect or unnecessary command and can be removed.
RUN pip3 install -r ./streamlitgcp/requirements.txt \
- This command installs Python packages listed in the requirements.txt file located in the ./streamlitgcp/ directory. These packages are dependencies required for the Streamlit application to run.
pip install --upgrade pip
- This command upgrades the pip package manager to the latest version. It ensures that the latest pip version is used for subsequent package installations.
EXPOSE 8080
- This instruction informs Docker that the container will listen on port 8080. It doesn’t actually publish the port; it’s more of a documentation step to indicate which ports the container is intended to use.
HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health
- This is a health check command that checks the health of the application. It uses curl to make an HTTP request to http://localhost:8501/_stcore/health. If the request fails, it indicates that the application is not healthy.
ENTRYPOINT ["streamlit", "run", "./streamlitgcp/app.py", "--server.port=8080", "--server.address=0.0.0.0"]
- This sets the entry point for the Docker container. When the container is started, it will execute the specified command. In this case, it runs the Streamlit application by invoking Streamlit run ./streamlitgcp/app.py. It also specifies the server port (8080) and address (0.0.0.0) to listen on, allowing external access to the Streamlit app.
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.
Deploying the script to Google cloud
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/
Next time, use gcloud run deploy --source .
to deploy the current directory.
- Select your deployment region
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
[
✓ 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