Python
2/9/2026
4 min read

How to Use Docker with Python

How to Use Docker with Python

Docker is now a standard tool for running Python applications on a regular basis across the development, testing, and production phases. Using Docker with Python means running your application inside a container instead of using it directly on your local machine. The container includes Python, your dependencies, and system libraries, all bundled together.

In this guide, we'll have a look at how you use Docker with Python in real projects. You’ll learn what to install, how to write Dockerfiles for Python apps, how to run containers, and how to avoid common mistakes.

Why Use Docker with Python?

When you work with Python, your application often depends on:

  • A specific Python version

  • OS-level packages (like libpq, curl, or build-essential)

  • Python dependencies from pip

What docker does is it bundles all of these into a single, reproducible environment.

With Docker, you can:

  • Run the same Python app on any machine

  • Standardize development across teams

  • Simplify deployment to servers and cloud platforms

  • Isolate dependencies between projects

This consistency reduces setup time and deployment errors. Also docker needs to be installed on your system before you can get anything done. Once it’s installed, Docker runs in the background and manages containers for you.


You can verify Docker is installed by running:

docker --version

Working with Python Docker Images

Docker images for Python are published on Docker Hub, and some of the most commonly used base images are:

  • python:3.12

  • python:3.12-slim

  • python:3.12-alpine

Creating a Python Dockerfile

A Dockerfile defines how your Python app is built and how it’s run.

Basic Dockerfile for a Python App

FROM python:3.12-slim

# Set working directory
WORKDIR /app

# Copy dependency file first (for caching)
COPY requirements.txt .

# Install dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Copy application code
COPY . .

# Expose the app port
EXPOSE 5000

# Run the application
CMD ["python", "app.py"]

Why This Structure Matters

  • Copying requirements.txt first allows Docker to cache dependencies

  • -no-cache-dir helps to keep the image size smaller

  • WORKDIR ensures all commands run in the correct directory

Creating a Docker image

From the project root, run:

docker build -t python-docker-app .
  • t assigns a name (tag) to the image

  • . tells Docker to use the current directory


You can list images with:

docker images

Executing a Python container

Start your container with:

docker run -p 5000:5000 python-docker-app
  • p 5000:5000 maps the container port to your local machine

  • The app becomes available at http://localhost:5000

At this point, your Python app is fully containerized.

Using Python and Docker with Environment Variables

One common error is hardcoding configuration values. Docker provides clean support for environment variables.

Example: Passing an Environment Variable

docker run -p 5000:5000 -e FLASK_ENV=production python-docker-app

In Python:

import os

env = os.getenv("FLASK_ENV", "development")

This pattern is essential for secure and scalable deployments.

Accelerating Docker Builds for Python

Poor layering is a common cause of slow Docker builds, and to avoid that

  1. Pin dependency versions in requirements.txt

  2. Avoid copying unnecessary files

  3. Use a .dockerignore file


Example .dockerignore

__pycache__/
.env
.git
venv/

This prevents large or sensitive files from entering your image.

Using Python for Development in Docker

For development, you often want live code reloading.

Mounting Source Code

docker run -p 5000:5000 -v $(pwd):/app python-docker-app

This allows you to edit code locally while the container runs. This approach is common in local development but not recommended for production.

Dockerizing Different Python Workloads

Docker works beyond web apps.

Background Workers

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

Scripts and CLI Tools

docker run --rm python-docker-app python script.py

FastAPI Apps

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

The Docker fundamentals remain the same.

Typical Errors to Avoid

  • Using latest Python images in production

  • Installing system packages without cleanup

  • Running containers as root when unnecessary

  • Rebuilding images on every code change due to poor caching

Avoiding these issues leads to smaller, faster, and safer containers.

When Docker Makes the Biggest Difference

Docker is valuable when:

  • You work in a team with different OS environments

  • You deploy Python apps frequently

  • You manage multiple Python services

  • You need predictable CI/CD pipelines

Making use of Docker with Python is no longer optional for modern software teams. It gives you control over environments, reduces friction between development and production, and scales well from solo projects to enterprise systems.

Enjoyed this article?

Subscribe to our newsletter for more backend engineering insights and tutorials.