Have you ever wanted to package your Machine Learning models into a portable container that works anywhere?
Today, I'll show you exactly how I did it — by training a Logistic Regression model on the Iris dataset, visualizing the data, and packaging everything neatly into a Docker image.
Let's dive in! 🏊♂️
📦 Project Structure
Here’s how the project folder is organized:
ml-docker-project/
├── Dockerfile
├── model.py
└── Iris.csv
🐳 Dockerfile Breakdown
Here's the Dockerfile
that powers the container:
# Use the official Python image as a base
FROM python:3.12-slim
# Set working directory inside the container
WORKDIR /app
# Copy all project files into the container
COPY . .
# Install required Python packages
RUN pip install --no-cache-dir pandas scikit-learn matplotlib joblib
# Run model.py on container start
CMD ["python", "model.py"]
✅ What this Dockerfile does:
- Uses a minimal Python 3.12 base image.
- Copies all project files.
- Installs only essential Python libraries.
- Runs the model training script (
model.py
) when the container starts.
🧪 What model.py
Does
Here’s what happens inside the model.py
script:
-
Load the Iris dataset from
Iris.csv
. - Preview dataset shape and first few rows.
-
Visualize the data with:
- Histograms
- Density plots
- Prepare features and labels for modeling.
- Split the data into training and testing sets (67%-33%).
- Train a Logistic Regression model using scikit-learn.
- Evaluate model accuracy and print the result.
-
Save the trained model to
logistic_model.pkl
.
All plots are saved silently as .png
files (histograms.png
, density_plots.png
) for future reference. 📊
🔨 How to Build, Tag, and Push the Docker Image
Here’s the step-by-step to package and publish the image:
1️⃣ Build the Docker Image
docker build -t your_dockerhub_username/your_image_name:latest .
(Example: docker build -t viratpk18/24mcr078:latest .
)
2️⃣ Log in to DockerHub
docker login
3️⃣ Push the Image to DockerHub
docker push your_dockerhub_username/your_image_name:latest
📤 DockerHub Image Description
When you publish, you can use a description like this:
Title: Logistic Regression on Iris Dataset - Dockerized
Description:
Train a Logistic Regression model on the classic Iris dataset inside a container!
- Loads and explores the Iris dataset
- Visualizes data (histograms, density plots)
- Splits into train/test
- Trains a Logistic Regression model
- Evaluates and prints accuracy
- Saves the trained model (
logistic_model.pkl
)Fully reproducible with Docker. Just pull and run!
Pull Command:
docker pull your_dockerhub_username/your_image_name:latest
Run Command:
docker run your_dockerhub_username/your_image_name:latest
💬 Final Thoughts
With just a few simple steps, we can turn a machine learning project into a portable, reproducible, and shareable artifact! 🚀
By using Docker, you ensure that your model can be trained, evaluated, and tested in any environment, without worrying about Python versions, library installations, or messy dependencies.
This is just the beginning — next steps could involve:
- Adding a Flask API on top to serve the model.
- Building a simple web UI for predictions.
- Automating deployments with GitHub Actions.
The possibilities are endless!
If you found this helpful, follow me for more Machine Learning and DevOps content! 💬
Happy Dockering! 🐳