sethserver.com Subscribe
Docker for Python Web Apps: From Flask Hello World to Production Deployment | Complete Guide

Docker for Python Web Apps: From Flask Hello World to Production Deployment | Complete Guide

By Seth Black • Updated: March 18, 2026

Programming · 8 min read

Like this kind of writing? Get one email a week with notes on startups, AI, and the occasional strong opinion about Python: subscribe to the newsletter.

Docker helps because it turns a lot of "mystery meat environments" into something you can point at and say: this is the thing we run.

I don't start with "let's architect a solution." I start with: Who's struggling? What's broken? Where's the code? What's on fire? Not in a dramatic way. Most fires have a short list of causes. A Flask app has maybe ten common failure modes. Same with databases. The list is short.

Talk to people fast and you find the real expert, the person hiding in the corner, and the person who built this thing alone and then quit.

I started using Docker around 2014, back when it still felt like a magic trick. Before that, I did the VM dance: build a base image, patch it, hand it to someone else, wait for it to drift, then patch it again. VMs work, but they invite long-lived pets. Containers push you toward cattle. Security folks like Docker too. Nothing brings people together like a spicy CVE (Log4Shell, I'm looking at you).

Why Docker still matters

Before Docker, plenty of teams did VMs. I did too. They work, but they're heavy, slow to rebuild, and they encourage a kind of "snowflake server" culture. You SSH in, someone "just installed a thing," and now the server is unique. Next month you rebuild it and nothing matches.

Docker got popular because it made isolation and packaging feel normal. You build an image, you run a container, and the app behaves the same way on your laptop and in prod.

That's the "works on my machine" problem. It's rarely the code. It's the environment:

Docker gives you developer confidence. Not the "I'm a genius" kind. The boring kind. Process confidence.

Here's a real example: a team pins Flask==2.0.1 but leaves Werkzeug unpinned. On one machine it pulls a compatible version. In CI it pulls a newer one and breaks some import path. Docker won't fix bad dependency hygiene, but it will make the break consistent and reproducible. That's a big deal.

Docker is also handy even if you never run containers in production. If you can run the app and database locally with one command, onboarding gets easier, bug reports get sharper, and "I can't reproduce it" shows up less.

A tiny Flask app, containerized

Let's build the smallest useful example. Create app.py:

from flask import Flask

app = Flask(__name__)

@app.get("/")
def hello():
    return "Hello, Docker!\n"

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000)

And requirements.txt:

Flask==2.0.1

Now the Dockerfile:

FROM python:3.9-slim

WORKDIR /app
COPY . /app

RUN pip install --no-cache-dir -r requirements.txt

EXPOSE 5000
CMD ["python", "app.py"]

Build and run it:

docker build -t python-flask-app .
docker run -p 5000:5000 python-flask-app

Hit http://localhost:5000 and you should get your greeting.

Quick note on 0.0.0.0: if you bind Flask to 127.0.0.1 inside the container, the port mapping won't help you. Binding to 0.0.0.0 means "listen on all interfaces," which is what you want inside a container.

A few things that matter in real projects:

Common pattern:

```dockerfile
FROM python:3.9-slim

WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY . .
EXPOSE 5000
CMD ["python", "app.py"]
```

Example (Flask + gunicorn):

requirements.txt:

txt Flask==2.0.1 gunicorn==20.1.0

Dockerfile command:

dockerfile CMD ["gunicorn", "-b", "0.0.0.0:5000", "app:app"]

Example:

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

bash docker ps docker exec -it <container_id> /bin/sh python --version pip freeze | head

If you can't inspect the running container, you're guessing.

Docker Compose: the moment it gets real

Most apps aren't just a web process. They have a database, maybe Redis, maybe a worker. Compose is the "stop arguing, run the stack" tool.

Here's a basic docker-compose.yml for Flask + Postgres:

version: "3"
services:
  web:
    build: .
    ports:
      - "5000:5000"
  db:
    image: postgres
    environment:
      POSTGRES_USER: db_user
      POSTGRES_PASSWORD: db_pass

Run it:

docker-compose up

Now you've got two services, isolated, reproducible, and easy to nuke and rebuild. If your environment can't be destroyed and recreated quickly, you don't have an environment. You have a pet.

This Compose file is still "hello world" though. A few upgrades make it actually usable:

1) Add a database name and a volume

If you don't set POSTGRES_DB, Postgres will pick a default. If you don't mount a volume, your data disappears when the container is removed. Sometimes that's fine. Often it's not.

version: "3"
services:
  web:
    build: .
    ports:
      - "5000:5000"
    environment:
      DATABASE_URL: postgres://db_user:db_pass@db:5432/app_db
    depends_on:
      - db

  db:
    image: postgres:15
    environment:
      POSTGRES_USER: db_user
      POSTGRES_PASSWORD: db_pass
      POSTGRES_DB: app_db
    volumes:
      - pgdata:/var/lib/postgresql/data

volumes:
  pgdata:

Now your Flask app can connect to db because Compose gives you a DNS name equal to the service name. That tiny feature saves a lot of time.

2) Install the Postgres driver

Your container needs psycopg2 or psycopg to talk to Postgres. This is where Docker forces you to be honest about system packages.

If you use psycopg2, you need build tools:

RUN apt-get update && apt-get install -y --no-install-recommends gcc libpq-dev \
    && rm -rf /var/lib/apt/lists/*

Trade-off: build tools increase image size and build time. If you use psycopg2-binary you can often skip the compiler, but you should know what you're shipping. For stricter environments, build it clean.

3) "Depends on" isn't "ready"

depends_on only controls start order. Postgres might still be booting when Flask tries to connect. In real apps, you handle this with retries, a health check, or a small "wait-for-db" script.

Minimal retry logic in Python usually works. The goal is fewer flaky starts.

4) Dev ergonomics: mount code, don't rebuild every time

If you're iterating quickly, rebuilding the image on every code change gets old. For local dev, you can mount your code into the container:

services:
  web:
    build: .
    ports:
      - "5000:5000"
    volumes:
      - .:/app

Trade-off: you've mixed host and container filesystems. It works well, but you can hit permission issues on Linux, and file watching behaves differently across OSes. Still worth it for a lot of teams.

Deploying: pick your flavor of pain

Once you can build an image, most cloud paths look similar. The pain shifts.

A concrete ECS flow looks like this:

  1. Build your image locally (or in CI).
  2. Tag it for ECR.
  3. Push it to ECR.
  4. Create an ECS Task Definition that points at that image, sets env vars, and exposes ports.
  5. Run it as a Service behind a load balancer if it's a web app.

The key trade-offs:

No matter where you deploy, the win stays the same: you ship the same container you tested.

One more practical note: build images in CI for repeatability. Local builds are fine early on. The first time a teammate says "it works for me" and you can't reproduce their exact image, you'll want a clean pipeline. For a practical approach to automated deployments with containers, check out AWS LightSail: The Final Piece of the Replit to Production Puzzle.

Python is still my default for web apps

Python isn't the fastest language at runtime. I know. I've measured it. I still pick it because I can build quickly, the ecosystem is deep, and async frameworks like FastAPI (with asyncio) are great for I/O-heavy work.

A few real-world picks:

Async doesn't make Python magically fast. It makes it good at waiting. If your app spends most of its time talking to databases, calling other services, or waiting on network I/O, async can boost throughput without a pile of threads.

Speed is nice. Shipping something that works is better. What matters more than perfect architecture is recovering from catastrophic failure when things inevitably break.

-Sethers

Share this post
Newsletter

One email, once a week.

Notes on databases, systems, and the occasional strong opinion about Python. No spam, unsubscribe anytime.

Seth Black
Written by

Seth Black

Engineer and founder based in Texas. Writes about databases, AI, and running things in production. Embeds in small teams as lead engineer.

More from Programming

View all →
Programming

What's the Differences Between Relational, NoSQL, Vectors, and Caches

Apr 14, 2026
Programming

Git and Source Control in AI Land

Apr 14, 2026
Programming

Domains, DNS, and why it feels fragile

Apr 14, 2026