Building and Deploying a Simple Machine Learning App with Docker

In this post, I’ll walk you through how I built a simple machine learning app using Python, containerized it with Docker, and published the image to DockerHub. This setup ensures the app can be run on any machine without worrying about dependencies or environment setup — super handy for portability and sharing!

The Machine Learning App

The app trains a logistic regression model using the Iris dataset to classify flowers based on sepal and petal measurements. Here's what it does step-by-step:

  1. Loads the dataset (Iris.csv)

  2. Displays basic info (shape + preview)

  3. Generates and saves histograms and density plots

  4. Splits the data for training and testing

  5. Trains a logistic regression model using scikit-learn

  6. Evaluates accuracy on the test set

  7. Saves the model using Joblib

Make sure to keep Iris.csv in the same directory as app.py before running.

The Python Script (app.py)

from pandas import read_csv
from matplotlib import pyplot
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
import joblib
# Step 1: Load dataset
filename = "Iris.csv"
data = read_csv(filename)

# Step 2: Show data shape and preview
print("Shape of the dataset:", data.shape)
print("First 20 rows:\n", data.head(20))

# Step 3: Histograms
data.hist()
pyplot.savefig("histograms.png")
pyplot.close()

# Step 4: Density plots
data.plot(kind='density', subplots=True, layout=(3,3), sharex=False)
pyplot.savefig("density_plots.png")
pyplot.close()

# Step 5: Extract features and labels
array = data.values
X = array[:, 1:5]
Y = array[:, 5]

# Step 6: Train-test split
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.33, random_state=7)

# Step 7: Train model
model = LogisticRegression(max_iter=200)
model.fit(X_train, Y_train)

# Step 8: Evaluate
result = model.score(X_test, Y_test)
print("Accuracy: {:.2f}%".format(result * 100))

# Step 9: Save the model
joblib.dump(model, "logistic_model.pkl")

Writing the Dockerfile

To containerize the app, I created a simple Dockerfile that:

  • Starts from a lightweight Python 3.9 image
  • Installs all required dependencies
  • Copies the app files into the container
  • Runs the script on startup
FROM python:3.9-slim

RUN pip install pandas scikit-learn matplotlib joblib

COPY . .

CMD ["python", "app.py"]

Building the Docker Image

With everything in place, I opened a terminal and ran:

docker build -t vaishnavi8754/24mcr121-ml:latest .

Docker pulled the base image, installed the dependencies, copied the files, and built the image successfully.

Pushing the Image to DockerHub

After logging into DockerHub using:

docker login

I pushed the image using:

docker push vaishnavi8754/24mcr121-ml:latest

Now the image is available at:

🔗 https://hub.docker.com/r/vaishnavi8754/24mcr121-ml

You (or anyone) can run it with:

docker pull vaishnavi8754/24mcr121-ml:latest
docker run vaishnavi8754/24mcr121-ml:latest

Image description

Image description

Image description