How to Create a Docker Image and Push It to DockerHub

Perfect!
You updated the Python file to hello_world_ml.py and added a machine learning logic using Iris dataset, logistic regression, and saving a model.

I’ll now update the full Dev.to article accordingly.
Here’s the new polished version ready for yo…


This content originally appeared on DEV Community and was authored by Gowtham

Perfect!

You updated the Python file to hello_world_ml.py and added a machine learning logic using Iris dataset, logistic regression, and saving a model.

I'll now update the full Dev.to article accordingly.

Here’s the new polished version ready for your post: 🚀

🚀 How to Dockerize a Simple Machine Learning App and Push It to DockerHub

Hey devs! 👋

In this post, I'll show you how to create a Docker image for a simple Machine Learning Python app and push it to DockerHub. 🚀

We’ll create a small ML project using the Iris dataset, logistic regression, and model saving.

Let’s get started! 🐳

1️⃣ Create a Python App

First, create a Python file named hello_world_ml.py with the following code:

python
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: Display data shape and preview

print("Shape of the dataset:", data.shape)
print("First 20 rows:\n", data.head(20))

Step 3: Plot and save histograms silently

data.hist()
pyplot.savefig("histograms.png")
pyplot.close()

Step 4: Plot and save density plots silently

data.plot(kind='density', subplots=True, layout=(3,3), sharex=False)
pyplot.savefig("density_plots.png")
pyplot.close()

Step 5: Convert to NumPy array and extract features/labels

array = data.values
X = array[:, 1:5] # Features
Y = array[:, 5] # Labels

Step 6: Split data into training and testing

test_size = 0.33
seed = 7
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=test_size, random_state=seed)

Step 7: Create and train logistic regression model

model = LogisticRegression(max_iter=200)
model.fit(X_train, Y_train)

Step 8: Evaluate and display accuracy

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

Step 9: Save the trained model to a file

joblib.dump(model, "logistic_model.pkl")

✅ Make sure you also have the Iris.csv dataset in the same folder.

2️⃣ Create a Dockerfile

Next, create a file named Dockerfile in the same directory:

dockerfile

Use a lightweight Python image

FROM python:3.12-slim

Set the working directory

WORKDIR /app

Copy all files into the container

COPY . .

Install required libraries

RUN pip install pandas matplotlib scikit-learn joblib

Define the command to run the app

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

Explanation:

  • Base image: Python 3.12 slim.
  • Working directory: /app.
  • Installs all necessary Python libraries.
  • Runs the hello_world_ml.py script when container starts.

3️⃣ Build the Docker Image

Open your terminal, navigate to your project folder, and run:

bash
docker build -t your-dockerhub-username/ml-docker-app:latest .

Example:

bash
docker build -t myusername/ml-docker-app:latest .

✅ This command builds the Docker image and tags it.

4️⃣ Login to DockerHub

If not already logged in, run:

bash
docker login

Enter your DockerHub username and password when prompted.

5️⃣ Push the Docker Image to DockerHub

After building and logging in, push the image:

bash
docker push your-dockerhub-username/ml-docker-app:latest

Example:

bash
docker push Gowtham/ml-docker-app:latest

✅ This uploads your image to your DockerHub repository.

6️⃣ Pull and Run the Image from Anywhere

Anyone can now pull and run your ML app like this:

bash
docker pull Gowtham/ml-docker-app:latest
docker run Gowtham/ml-docker-app:latest

🎯 The container will load the Iris dataset, train a logistic regression model, save graphs, and display model accuracy!

Example output:

Shape of the dataset: (150, 6)
First 20 rows:
Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species
0 1 5.1 3.5 1.4 0.2 Iris-setosa
...
Accuracy: 94.00%

🔥 Final Tips

  • Add .dockerignore to avoid copying unnecessary files.
  • Keep your Docker images lightweight.
  • Make sure your Iris.csv is included while copying files (COPY . . does this).
  • You can extend this with Flask API for serving predictions!

📢 That's it!

You now know how to Dockerize a Machine Learning Python app and share it worldwide via DockerHub. 🐳✨

If you found this helpful, smash that ❤ and share it with your dev friends!

Until next time, Happy Coding! 🚀

Would you also like me to give you some bonus ideas 💡 to extend this post (like "how to serve the trained model using Flask inside Docker") to make an even bigger article series on Dev.to? 🎯

It could really help you build a following! 🚀


This content originally appeared on DEV Community and was authored by Gowtham


Print Share Comment Cite Upload Translate Updates
APA

Gowtham | Sciencx (2025-04-28T17:52:55+00:00) How to Create a Docker Image and Push It to DockerHub. Retrieved from https://www.scien.cx/2025/04/28/how-to-create-a-docker-image-and-push-it-to-dockerhub/

MLA
" » How to Create a Docker Image and Push It to DockerHub." Gowtham | Sciencx - Monday April 28, 2025, https://www.scien.cx/2025/04/28/how-to-create-a-docker-image-and-push-it-to-dockerhub/
HARVARD
Gowtham | Sciencx Monday April 28, 2025 » How to Create a Docker Image and Push It to DockerHub., viewed ,<https://www.scien.cx/2025/04/28/how-to-create-a-docker-image-and-push-it-to-dockerhub/>
VANCOUVER
Gowtham | Sciencx - » How to Create a Docker Image and Push It to DockerHub. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/04/28/how-to-create-a-docker-image-and-push-it-to-dockerhub/
CHICAGO
" » How to Create a Docker Image and Push It to DockerHub." Gowtham | Sciencx - Accessed . https://www.scien.cx/2025/04/28/how-to-create-a-docker-image-and-push-it-to-dockerhub/
IEEE
" » How to Create a Docker Image and Push It to DockerHub." Gowtham | Sciencx [Online]. Available: https://www.scien.cx/2025/04/28/how-to-create-a-docker-image-and-push-it-to-dockerhub/. [Accessed: ]
rf:citation
» How to Create a Docker Image and Push It to DockerHub | Gowtham | Sciencx | https://www.scien.cx/2025/04/28/how-to-create-a-docker-image-and-push-it-to-dockerhub/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.