Whether you're a developer automating deployments or a student getting started with DevOps, learning how to create Docker images and push them to DockerHub is essential.

In this post, we’ll walk through:

  1. Writing a simple Dockerfile
  2. Building a Docker image
  3. Tagging the image
  4. Pushing it to DockerHub

Let’s get started!

What is a Dockerfile?

A Dockerfile is a plain text file that contains instructions on how to build a Docker image. Think of it as a recipe — it tells Docker how to build an environment tailored for your application.

Dockerfile

FROM python:3.14.0a7-alpine3.21

# Install required system dependencies
RUN apk add --no-cache build-base musl-dev linux-headers

# Install Python libraries
RUN pip install --upgrade pip && \
    pip install pandas scikit-learn matplotlib

# Copy and run your script
COPY . .
CMD [ "python", "hello_world_ml.py" ]

1. Build the Docker Image

docker build -t DockerUserName/dockerfilename:latest .

2. Run the Docker Image

docker run --rm DockerUserName/dockerfilename:latest

3. List Docker Images

docker image ls

4. Push the Image to DockerHub

docker push DockerUserName/dockerfilename:latest