Published
- 4 min read
Streamlit Deployment Guide Part 1: Containerization
When I needed to create a quick proof of concept (PoC) using Streamlit, I faced two constraints:
- Self-hosted instead of using Streamlit Community Cloud.
- Control access to the deployed website.
With this in mind, containerizing the application and hosting it on Azure was the best solution with my skillset. This post kicks off a series detailing the process, broken down into the following parts:
- Part 1: Containerizing a Streamlit app. You are here š
- Part 2: GitHub Workflow for Building and Publishing to ghcr.io
- Part 3: Azure Infrastructure via Terraform
- Part 4: GitHub Workflow for Terraform Apply & Destroy
Do you want to deploy your Streamlit application on Azure right now? Use the template repository š
TL;DR
Read the completed Dockerfile, execute commands to build the image and run the container.
Streamlit Application Files
Before proceeding with the steps, ensure the following Streamlit application files are available. You can find these files in the app folder of the GitHub repository.
.streamlit/config.toml
[client]
showErrorDetails = false
toolbarMode = "auto"
showSidebarNavigation = true
[theme]
base = "dark"
[logger]
level = "debug"
messageFormat = "%(asctime)s %(message)s"
[server]
port = 80
[browser]
gatherUsageStats = false
app.py
import os
import streamlit as st
st.title("Streamlit on Azure š")
st.header('Running on a Web App in a Container š³', divider='rainbow')
requirements.txt
streamlit
Create a Dockerfile
Note: Docker is a prerequisite, read the install documentation for more details.
Completed Dockerfile
FROM python:3.9-slim
LABEL maintainer="Wayne Goosen" \
version="1.0.0" \
description="Streamlit template for Docker. Uses app.py as the main file."
WORKDIR /app
RUN apt-get update \
&& rm -rf /var/lib/apt/lists/*
COPY requirements.txt .
RUN pip3 install -r requirements.txt
COPY .streamlit/config.toml .streamlit/
COPY app.py .
RUN groupadd -g 1005 appgroup && \
useradd -u 1005 -g appgroup appuser && \
chown -R appuser:appgroup /app
EXPOSE 80
USER appuser
ENTRYPOINT ["streamlit", "run"]
CMD ["app.py"]
Dockerfile Walkthrough
Specify Base Image
FROM python:3.9-slim
- Uses a slim version of Python 3.9 as the base image, ensuring a minimal and efficient environment.
Add Metadata Labels
LABEL maintainer="Wayne Goosen" \
version="1.0.0" \
description="Streamlit template for Docker. Uses app.py as the main file."
- Provides metadata about the Docker image, including the maintainer, version, and description.
Set Working Directory
WORKDIR /app
- Sets the working directory inside the container to
/app
. - Note: Streamlit version 1.10.0 and higher, Streamlit apps cannot be run from the root directory of Linux distributions.
Update Package List
RUN apt-get update \
&& rm -rf /var/lib/apt/lists/*
- Updates the package list for the APT package manager and then removes the cached package lists to reduce image size.
Copy Requirements File
COPY requirements.txt .
- Copies
requirements.txt
from the host machine to the current working directory in the container (/app
).
Install Python Dependencies
RUN pip3 install -r requirements.txt
- Installs the Python dependencies listed in
requirements.txt
usingpip
.
Copy Configuration and Application Files
COPY .streamlit/config.toml .streamlit/
COPY app.py .
- Copies the Streamlit configuration file and the main application file (
app.py
) into the container. Separated to support caching.
Create Dedicated Application User and Group
RUN groupadd -g 1005 appgroup && \
useradd -u 1005 -g appgroup appuser && \
chown -R appuser:appgroup /app
- Creates a new group
appgroup
with GID 1005. - Creates a new user
appuser
with UID 1005 and adds it toappgroup
. - Changes the ownership of the
/app
directory toappuser:appgroup
.
Expose Port
EXPOSE 80
- Informs Docker that the container will listen on port 80 at runtime.
Switch to Non-Root User
USER appuser
- Switches to the newly created
appuser
to run the application, enhancing security.
Set Entry Point and Command
ENTRYPOINT ["streamlit", "run"]
CMD ["app.py"]
- Sets the entry point (command that runs when a container starts) to
streamlit run
, and the default command toapp.py
, which runs the Streamlit application. This approach allows you to run the container with your custom parameters tostreamlit run
Build the Docker Image
To build the Docker image, navigate to the directory containing the Dockerfile and run the following command:
docker build -t streamlit-app .
TheĀ -tĀ flag is used to tag the image. Here, we have tagged the imageĀ streamlit-app. If you run:
docker images
You should see aĀ streamlit-appĀ image under the REPOSITORY column. For example:
REPOSITORY | TAG | IMAGE ID | CREATED | SIZE |
---|---|---|---|---|
streamlit-app | latest | 70b0759a094d | About a minute ago | 628MB |
Run the Docker Container
To run the Docker container, use the following command:
docker run -p 8501:80 streamlit-app:latest
To view your app, browse toĀ http://0.0.0.0:8501Ā orĀ http://localhost:8501
Streamlit Configuration
In order to provide Streamlit configuration, there are two approaches:
- config.toml file (What is TOML?)
- command line parameters
In this example it uses the config.toml and you can find more configuration options in the documentation. Below I will showcase an example setting the port below using both approaches.
Config.toml
[server]
port = 80
Parameters
streamlit run app.py --server.port=80