Dockerize Streamlit App and making it production ready!
Last week at Bitscrunch we got a task to deploy Streamlit application for a quick demo. On first google search found it to be very easy but then it was NOT ! This tutorial is the outcome of extra efforts spend while searching for solutions. Hope it helps others !
Prerequisite:
- A working streamlit application
- A linux machine with Docker installed
- 15min of time.
Lets Begin the fun
Step 1: Let’s create a Docker file.
In the working root directory, let’s create a file named ‘Dockerfile’ without any extensions. Your file structure should be as shown.
root
├── Dockerfile
├── something.png
├── something.txt
├── model
├── nainvemodel.pkl
Step 2: Dockerfile’s content
FROM python:3.7
RUN apt-get update && apt-get install -y python3-opencv
WORKDIR /app
COPY requirements.txt ./requirements.txt
RUN pip3 install opencv-python cython numpy
RUN pip3 install -r requirements.txt
EXPOSE 8501
COPY . /app
CMD streamlit run filename.py --server.enableCORS false --server.enableXsrfProtection false
NOTE: This is the most critical part. If you don’t add --server.enableCORS false --server.enableXsrfProtection false
then the webpage would be stuck at loading screen
Step 3: Build the docker image
docker build -t streamlitapp:latest .
Step 4: View docker images.
docker image ls
Step 5: Running as a container
docker run -p 8501:8501 streamlitapp:latest
Output:
It also starts our streamlit app in the following urls:
Network URL: http://172.17.0.2.8501
External URL: https://193.106.63.249:8501
With that, the Streamlit app is now deployed with docker.
Source code : https://github.com/chetanbothra/streamlit-docker
If this article has helped you, Please share your feedback via DM or connect with me on https://www.linkedin.com/in/chetanbothra