Skip to content

Docker Postgres Setup

To move beyond in-memory dictionaries, we need a real database. We'll use PostgreSQL, which is the industry standard for production web applications.

Instead of installing PostgreSQL directly on your operating system (which can cause version conflicts and clutter your machine), we'll run it inside a Docker container.


Creating the Docker Compose File

Create a file named compose.yaml in the root of your project:

compose.yaml
services:
  db:
    image: postgres:17-alpine
    ports:
      - "5432:5432"
    environment:
      POSTGRES_DB: release_tracker
      POSTGRES_USER: release_tracker
      POSTGRES_PASSWORD: release_tracker
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U release_tracker -d release_tracker"]
      interval: 10s
      timeout: 5s
      retries: 5
    volumes:
      - postgres_data:/var/lib/postgresql/data

volumes:
  postgres_data:

We're mapping port 5432 from the container to our local machine so our Python app can connect to it. We also define a persistent volume (postgres_data) so our data isn't lost if the container stops.

Run the container in the background using:

shell
docker compose up -d

A Note on Docker Images

You'll notice we're using the postgres:17-alpine image. alpine is a highly minimized Linux distribution.

Similarly, when we eventually containerize our Python application for production, we'll use a slim image (like python:3.14-slim). A slim image is a variant of the official Python base image built on a Debian Slim Linux distribution. It provides a great balance between size and compatibility by including only essential packages. It's significantly smaller than the standard Python image because it excludes compilers, development tools, and documentation. This smaller footprint speeds up deployment, reduces the vulnerability surface area, and improves overall security.