Containerizing a Streamlit App with Docker
A Step-by-Step Guide on how to containerize a streamlit web app using Docker.

Hello! I'm Subodh, I'm passionate about technology and security, focusing on cloud security ☁️🔒, AWS 🚀, and artificial intelligence 🤖.
Have you ever built a machine learning project and wanted to share it with the world? In this blog, I’ll walk you through how I containerized my Streamlit app for lung cancer detection using Docker. This allows me to easily deploy it anywhere, ensuring consistency and portability. Let’s dive into the process!
What’s the Project About?
I developed a machine learning model that predicts lung cancer (Adenocarcinoma, Large cell carcinoma, Squamous cell carcinoma, or Normal) using CT scan images. The goal was to make it accessible via a web interface where users can upload their CT scans for prediction.
To streamline deployment, I decided to use Docker to containerize the app.
Why Docker?
Docker makes it easy to package my app with all its dependencies, eliminating the classic “works on my machine” problem. By using Docker, my Streamlit app becomes portable and can run consistently across any environment.
Steps to Containerize the Streamlit App
Install Docker : First, make sure Docker is installed and running on your machine. If not, you can download and install it from the official Docker website.
Create a Dockerfile: A
Dockerfileis a text document that contains the instructions to build your container image. Here’s the one I used:
# Base image with Python
FROM python:3.9-slim
# Set the working directory
WORKDIR /app
# Copy the current directory contents to /app in the container
COPY . /app
# Install the required dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Expose port 8501 for Streamlit
EXPOSE 8501
# Command to run the app
CMD ["streamlit", "run", "app.py"]
This file tells Docker to use Python 3.9, copy my app into the container, install dependencies from requirements.txt, and then run my Streamlit app.
3. Build the Docker Image: Now that the Dockerfile is ready, it’s time to build the Docker image. Open a terminal in the project directory and run:
docker build -t my-streamlit-app .
This command builds the image and tags it as my-streamlit-app.
4. Run the Container: Once the image is built, you can run your app in a container:
docker run -p 8501:8501 my-streamlit-app
This maps port 8501 on your machine to the container’s port 8501, allowing you to access the Streamlit app in your browser at http://localhost:8501.
5. Push to Docker Hub (Optional): To share your container with others or deploy it to the cloud, you can push the image to Docker Hub:
docker tag my-streamlit-app <your-dockerhub-username>/my-streamlit-app
docker push <your-dockerhub-username>/my-streamlit-app
Now, anyone can pull and run your app with a single command!
Snapshots of My Project

Web App deployed on Streamlit

Docker image is created on Dockerhub
Wrapping Up
Containerizing my Streamlit app using Docker was an eye-opener. It simplified deployment and made it easy to share with others. If you’re working on similar projects and want to ensure your app runs smoothly in any environment, I highly recommend giving Docker a try.
You can can clone my Github repository for easy access!
Have you containerized your app before? Share your thoughts and experiences in the comments below!





