DEV Community

Jane for Mastering Backend

Posted on • Originally published at blog.masteringbackend.com on

How to Use Docker with Python

title

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
Enter fullscreen mode Exit fullscreen mode

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"]
Enter fullscreen mode Exit fullscreen mode

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 .
Enter fullscreen mode Exit fullscreen mode
  • t assigns a name (tag) to the image
  • . tells Docker to use the current directory

You can list images with:

docker images
Enter fullscreen mode Exit fullscreen mode

Executing a Python container

Start your container with:

docker run -p 5000:5000 python-docker-app
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

In Python:

import os

env = os.getenv("FLASK_ENV", "development")
Enter fullscreen mode Exit fullscreen mode

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/
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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"]
Enter fullscreen mode Exit fullscreen mode

Scripts and CLI Tools

docker run --rm python-docker-app python script.py
Enter fullscreen mode Exit fullscreen mode

FastAPI Apps

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
Enter fullscreen mode Exit fullscreen mode

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.

Have a great one!!!

Author: Toluwanimi Fawole


Thank you for being a part of the community

Before you go:

Whenever you’re ready

There are 4 ways we can help you become a great backend engineer:

  • The MB Platform: Join thousands of backend engineers learning backend engineering. Build real-world backend projects, learn from expert-vetted courses and roadmaps, track your learning and set schedules, and solve backend engineering tasks, exercises, and challenges.
  • The MB Academy: The “MB Academy” is a 6-month intensive Advanced Backend Engineering Boot Camp to produce great backend engineers.
  • Join Backend Weekly: If you like posts like this, you will absolutely enjoy our exclusive weekly newsletter, sharing exclusive backend engineering resources to help you become a great Backend Engineer.
  • Get Backend Jobs: Find over 2,000+ Tailored International Remote Backend Jobs or Reach 50,000+ backend engineers on the #1 Backend Engineering Job Board.

Originally published at https://blog.masteringbackend.com.


Top comments (0)