Build Streamlit Docker Image and Deploy Publicly in 3 Minutes
open source
Aug 23, 2023

Build Streamlit Docker Image and Deploy Publicly in 3 Minutes

Ce Gao
Co-founder

Streamlit is an open-source Python library that makes it easy to create and share beautiful, custom web apps for machine learning and data science. In just a few minutes you can build and deploy powerful data apps.

In this blog post, we'll show you how to build a Streamlit Docker image and deploy it publicly in 3 minutes. The deployment will give you a public URL (similar to xx.modelz.live) for your model that anyone can use without installing anything on their computer.

After that, you can share your model with others and get feedback on it!

First, create a Streamlit app

It's easy to create a Streamlit app. You can follow the official tutorial. Here is an example:

import streamlit as st
import pandas as pd
import numpy as np

st.title('Uber pickups in NYC')

Build a Streamlit Docker image

After creating a Streamlit app, you can build a Streamlit Docker image. We provide a Dockerfile for CPU deployment and a Dockerfile.gpu for GPU. You can use them directly.

The Dockerfile is very simple:

FROM python:3.9

WORKDIR /workspace

COPY . /workspace/

RUN pip install -r /workspace/requirements.txt

CMD ["streamlit", "run", "main.py", "--server.port=8501", "--server.address=0.0.0.0","--server.enableCORS=false", "--server.enableXsrfProtection=false"]

You just need to update the requirements.txt and main.py to your own. Then you could run the command to push it to the Docker Hub:

docker build -t docker.io/USER/IMAGE .
docker push docker.io/USER/IMAGE

Deploy to any cloud provider

After building the Streamlit Docker image, you can deploy it. You could pick up a VM from any cloud provider, such as AWS, GCP, Azure, etc. Then you could run the command to deploy it:

$ pip install openmodelz
$ mdz start <your public ip>
$ mdz deploy --image docker.io/USER/IMAGE --port 7860

Then you will get a public URL for your model via mdz list, such as xx.modelz.live. OpenModelZ takes care of the rest for you!

Conclusion

In this blog post, we showed you how to build a Streamlit Docker image and deploy it publicly in 3 minutes. We hope that this will help you get started with Streamlit and make your models more accessible to others. If you have any questions or feedback, please let us know in our Discord server!