Are you looking for how to build a Python Docker image and use it? We’ve got you covered. A Python Docker image is a portable container image. It bundles a Python runtime and your app. You can use it to package Python code, dependencies, and runtime into one portable unit. That unit runs the same on a developer laptop, a CLI runner, or a production server. Moreover, it speeds up CI/CD and enables reliable scaling.
You can build a Python Docker image with a Dockerfile. You have to choose a base like slim, alpine, or full. Then, install dependencies, and publish the image to a registry like Docker Hub or GHCR.
Let’s dive together into the world of Python Docker images!
What is a Python Docker Image?
A Python Docker image is a prebuilt container filesystem. It includes Python and a minimal Linux userspace. You have to run it with Docker to get a consistent Python runtime on any machine.
Why it Matters:
- You can have the same Python everywhere.
- It is reproducible builds and easy rollbacks with tags.
- Clean isolation for security and dependency control.
How to Choose the Right Python Image?
You should pick an image that balances size, compatibility, and speed.
Quick guide
Get exclusive access to all things tech-savvy, and be the first to receive
the latest updates directly in your inbox.
python:3.12-slim
— Best balance of size and compatibility.python:3.12
(full) — Good for dev and debugging.python:3.12-alpine
— Small but may break some packages.- Distroless / gcr.io/distroless/python3 — Minimal, secure, harder to debug.
Comparison of Common Python Docker Images
Variant (examples) | Typical Use | Pros | Cons | When to choose |
---|---|---|---|---|
python:3.12 (full) | Dev & CI | Full Debian toolchain; easy native-builds | Larger size | You need compilers, system libs, or debugging tools |
python:3.12-slim | Most prod apps | Smaller, still Debian-based; great compatibility | Some build tools missing | Best default for web apps & APIs |
python:3.12-alpine | Very small images | Tiny footprint | musl libc can break some wheels; slower builds | Only if you know deps compile on Alpine |
python:<version>-bookworm | Predictable base | Current stable Debian | Slightly bigger than Alpine | Pin exact Debian for compliance |
gcr.io/distroless/python3 (advanced) | Security-focused prod | Minimal attack surface | Debugging harder | Security-first shops with mature observability |
Tip: If you’re unsure, start with
python:3.12-slim
How to Create a Docker Image for a Python Flask App on Ubuntu?
Here are the steps to create Docker image Python Flask in Ubuntu:
1. Project Layout
app/
├─ app.py
├─ requirements.txt
└─ Dockerfile
app.py
from flask import Flask
app = Flask(__name__)
@app.get("/")
def home():
return {"status": "ok", "msg": "Hello from Dockerized Flask!"}
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000)
requirements.txt
flask==3.0.0
gunicorn==21.2.0
2. Dockerfile (Ubuntu host is fine; image remains Debian-slim)
# ---- base ----
FROM python:3.12-slim
# Avoid interactive prompts & speed up installs
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1
# System deps (only what you need)
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
&& rm -rf /var/lib/apt/lists/*
# App directory
WORKDIR /app
# Use Docker cache: copy manifests first
COPY requirements.txt .
# Install deps (no cache, wheels preferred)
RUN pip install --upgrade pip \
&& pip install --no-cache-dir -r requirements.txt
# Copy source
COPY . .
# Expose and define the start command
EXPOSE 8000
CMD ["gunicorn", "-b", "0.0.0.0:8000", "app:app"]
3. Build and Run
# Build
docker build -t myflask:latest ./app
# Run
docker run -p 8000:8000 --name flask-app myflask:latest
4. Test
Finally, open http://localhost:8000
. You should see the JSON response.
How to Speed Up Builds and Keep Images Small?
Optimization | Why it helps | Snippet |
---|---|---|
Use -slim base | Smaller layers | FROM python:3.12-slim |
Order layers smartly | Caches pip installs | COPY requirements.txt . then pip install ... then COPY . . |
.dockerignore | Avoid copying venv, caches | Add: __pycache__/ , .git/ , *.pyc , venv/ |
Multi-stage builds | Drop compilers from final image | See next block |
Pin versions | Reproducibility/security | flask==3.0.0 |
Non-root user | Better security | RUN useradd -m app && USER app |
Healthcheck | Auto-restart on failure | `HEALTHCHECK CMD curl -f http://localhost:8000/ |
Multi-stage Build:
# builder
FROM python:3.12-slim as builder
WORKDIR /wheels
COPY requirements.txt .
RUN pip wheel --no-cache-dir --wheel-dir=/wheels -r requirements.txt
# final
FROM python:3.12-slim
WORKDIR /app
COPY --from=builder /wheels /wheels
RUN pip install --no-cache-dir --no-index --find-links=/wheels -r /wheels/requirements.txt
COPY . .
EXPOSE 8000
CMD ["gunicorn", "-b", "0.0.0.0:8000", "app:app"]

How to Pull and Install a Python Docker Image on Ubuntu?
You have to run this command to install and pull the Python Docker image on Ubuntu:
# If Docker is installed:
docker pull python:3.12-slim
docker run -it --rm python:3.12-slim python --version
If you need Docker first, install the Docker Engine from your distro’s repo or Docker’s official package. Then, run the re-pull.
How to Run a Dev Environment with Hot Reload?
You have to use bind mounts. So, changes on your host reflect in the container:
docker run -it --rm -p 8000:8000 \
-v "$PWD/app":/app \
-w /app \
python:3.12-slim \
bash -lc "pip install -r requirements.txt && flask --app app run --host 0.0.0.0 --port 8000 --debug"
Docker Compose (dev)
# compose.yaml
services:
web:
image: python:3.12-slim
working_dir: /app
command: bash -lc "pip install -r requirements.txt && flask --app app run --host 0.0.0.0 --port 8000 --debug"
ports: ["8000:8000"]
volumes:
- ./app:/app
How to Push a Python Image to Docker Hub?
docker login
docker tag myflask:latest yourname/myflask:1.0.0
docker push yourname/myflask:1.0.0
# Optional: also push :latest
docker tag yourname/myflask:1.0.0 yourname/myflask:latest
docker push yourname/myflask:latest
Best practice: Tag with app version and Python version (e.g.,
1.0.0-py3.12
).
How to Check Python Docker Image Size and Reduce it?
Here is how to check Python Docker image size and reduce it:

- Show image sizes:
docker images | grep myflask
- Reduce size: use
-slim
, remove build tools, multi-stage builds, andpip --no-cache-dir
.
- Audit layers:
docker history myflask:latest
.
How to Publish Python Docker Images to Docker Hub or GitHub?
You have to tag, log in, and push. Also, use semantic tags and include Python versions.
Commands:
docker tag myflask:1.0 youruser/myflask:1.0-py3.12
docker push youruser/myflask:1.0-py3.12
For GitHub Container Registry: ghcr.io/<user>/myflask:1.0
.
How to Scan Vulnerabilities?
You should scan images before shipping:
# With Trivy (example)
trivy image yourname/myflask:1.0.0
# With Grype (example)
grype yourname/myflask:1.0.0
How to Handle Environment Variables, Secrets, and Config?
Pass env vars at run time: docker run -e FLASK_ENV=production ...
Use .env
with Compose:
env_file: .env
How to Run Python Scheduled Jobs?
You have to create an image that runs a script and exits or use cron-like schedulers:
FROM python:3.12-slim
WORKDIR /job
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY job.py .
CMD ["python", "job.py"]
You should run it on a schedule with your CI, Kubernetes CronJob, or a host cron invoking docker run
.
Your Quick Command Cheatsheet
Task | Command |
---|---|
Pull official image | docker pull python:3.12-slim |
Run Python REPL | docker run -it --rm python:3.12-slim python |
Build your app | docker build -t myflask:latest . |
Run your app | docker run -p 8000:8000 myflask:latest |
List images | docker images |
Remove dangling layers | docker image prune |
How to Run Python Docker Images in AWS Lambda or Serverless?
You have to package your apps as a compatible container image. Then, follow the container image requirements. Moreover, use smaller base images and make sure the handler works with the Lambda runtime interface.
Role of CyberPanel with Python Docker Images

CyberPanel is a next-gen web hosting control panel. It simplifies hosting and reverse proxy for containerized apps. You can also use CyberPanel to deploy images on a VPS, set domains, manage SSL, and view logs. With CyberPanel, you get:
- Docker Manager: pull images, set ports/env, start/stop containers from a UI.
- One-click SSL & domains: route traffic to your container via reverse proxy.
- Logs & backups: view container logs, schedule backups, and restore quickly.
Here is a practical approach you can follow:
push image to Docker Hub → install CyberPanel on your VPS → add domain + SSL → create container from your image → map port 80/443 via reverse proxy.
People Also Ask
What is the official Python Docker image, and where can I find it?
The official repository is library/python
on Docker Hub. There are different variants available, like python:3.12
, python:3.12-slim
, and python:3.11
. You should pick a specific version for reproducible builds.
What should I use between slim and alpine for Python?
Use slim
if you rely on many wheels (NumPy, cryptography, etc.). Alpine’s musl libc can break prebuilt wheels and slow compiles. Alpine is fine for very small, pure-Python apps.
What is one of the smallest Docker Python images?
Alpine or scratch-based distroless images are the smallest. But the smallest is not always best for compatibility.
Wrapping Up!
To sum up, a Python Docker image makes Python apps portable, testable, and scalable. You should use python:3.12-slim for most cases. Also, follow layer ordering, scan for vulnerabilities, and publish images with clear tags. For web apps, you should deploy behind SSL and a reverse proxy.
Here is your next 10-minute plan:
- Copy the
Dockerfile (slim)
above. - Build and run:
docker build -t myflask . && docker run -p 8000:8000 myflask
. - Scan with Trivy and push to your registry.
- Deploy behind SSL with CyberPanel or your favorite platform.
Ready to ship? Turn your app into a portable image today—then tag, push, and go live!