Docker Series — Part 13: Building & Publishing a Python-Enabled Docker Image

Nitin DhimanNitin Dhiman
2 min read

This session marks a key milestone — we didn’t just run Python in a container, we built a fully functional Python environment from scratch, automated it with Dockerfile, and published it to Docker Hub

Step 1: Build the Foundation (CentOS + Python3)

We start by launching a base CentOS container and installing Python3:

docker run -it centos:7
yum install python3 -y

Python offers two options inside a container:

  • Use as a live interpreter: python3

  • Run saved code files: python3 myscript.py


Step 2: Write and Run Python Files

Create a sample Python file:

# my.py
x = 5
print("Hi, I’m", x)

You can either:

  • Copy it manually using docker cp

  • Or automate it using a Dockerfile


Step 3: Create a Dockerfile for Python + Scripts

FROM centos:7
RUN yum install python3 -y
RUN mkdir /code
COPY my.py /code/
CMD python3 /code/my.py

Now build the image:

docker build -t mypy:v1 .

And launch:

docker run -it mypy:v1
# Output: Hi, I’m 5

CMD vs ENTRYPOINT

Let’s talk automation:

  • CMD runs a default command, but can be overridden

  • ENTRYPOINT locks in a command (great for scripts or interpreters)

Want both? Use:

ENTRYPOINT ["python3"]
CMD ["/code/lw.py"]

Now:

  • Running docker run myimage will execute python3 /code/lw.py

  • You can pass another file as an argument if needed.


Add Metadata with MAINTAINER

A simple but pro move:

MAINTAINER Vimal Daga <vimal@lw.com>

Adds your name + contact inside the image.


Step 4: Push to Docker Hub

Login first:

docker login

Tag the image with your Docker ID:

docker tag mypy:v1 yourdockerhubusername/mypy:v1

Push it:

docker push yourdockerhubusername/mypy:v1

Now anyone can pull and use your custom Python Docker image — anywhere, anytime


What You’ve Learned

  • Installing Python in containers

  • Writing and copying code into containers

  • CMD vs ENTRYPOINT

  • Automating container behavior via Dockerfile

  • Adding metadata with MAINTAINER

  • Publishing to Docker Hub


Got any questions on building or pushing Docker images?
Let’s connect — happy to help you get hands-on with container automation!

0
Subscribe to my newsletter

Read articles from Nitin Dhiman directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nitin Dhiman
Nitin Dhiman

Self-taught DevOps enthusiast on a journey from beginner to pro. Passionate about demystifying complex tools like Docker, AWS, CI/CD & Kubernetes into clear, actionable insights. Fueled by curiosity, driven by hands-on learning, and committed to sharing the journey. Always building, always growing 🚀