Creating a Docker Image and Pushing to DockerHub for a Python ML App

coding #docker #python #machinelearning

In this post, I’ll walk through how I built a simple machine learning app using Python, packaged it into a Docker image, and then pushed that image to DockerHub. This setup makes it easy to run the app on any machine, without worrying about installing dependencies or setting up the environment.

The Machine Learning App
The app trains a logistic regression model on the Iris dataset to classify flowers based on sepal and petal measurements. It loads the data, prints basic info, generates and saves visualizations, splits the dataset, trains the model, evaluates accuracy, and saves the trained model using Joblib.

Make sure to place the Iris.csv file in the same directory as app.py so that the script can access it during execution.

Here's the Python file used:

Image description

Image description

Writing the Dockerfile
Once the app was working locally, the next step was to containerize it using Docker. For this, I created a simple Dockerfile that starts from a lightweight Python base image, installs the required dependencies, copies the code into the image, and defines the command to run the script when the container starts.

**
Image description**

This setup ensures that anyone who runs this image will get the same environment and behavior, no matter where they are.

Building the Docker Image
Once the Dockerfile was ready, I opened PowerShell, navigated to the project directory, and ran the following command:

Image description

Docker pulled the base image, installed the libraries, copied the code, and built the final image. The build process completed successfully, creating a local image tagged as shreevaishnavib/24mcr100-ml:latest

Image description

Pushing the Docker Image to DockerHub

With the image built locally, the next step was to make it available online by pushing it to DockerHub.

First, I logged into DockerHub from the terminal:

Image description

After authentication, I pushed the image to my repository:

Image description

Docker uploaded the image layers to the remote repository, and the image became publicly available at
https://hub.docker.com/r/shreevaishnavib/24mcr100-ml.
Now anyone can pull and run this image on their system using a single command.