DEV Community

Cover image for ๐Ÿš€ How I Deployed a Python Flask App with Docker and Troubleshot My Way to Victory ๐Ÿ› ๏ธ๐Ÿ๐Ÿณ
BAKRE JAMIU
BAKRE JAMIU

Posted on

๐Ÿš€ How I Deployed a Python Flask App with Docker and Troubleshot My Way to Victory ๐Ÿ› ๏ธ๐Ÿ๐Ÿณ

Deploying a Python Flask app with Docker sounds straightforwardโ€ฆ until it isnโ€™t. ๐Ÿ˜… Recently, I rolled up my sleeves to containerize and deploy a simple Flask app connected to PostgreSQL. While it seemed like a walk in the cloud, I ran into several wallsโ€”but breaking through them made the win even sweeter. Hereโ€™s how it all went down ๐Ÿ‘‡


๐Ÿ—๏ธ Project Setup

I started with a basic Flask app that connects to a PostgreSQL database. My directory looked like this:

๐Ÿ“ python-docker-project/
โ”œโ”€โ”€ Dockerfile
โ”œโ”€โ”€ compose.yml
โ”œโ”€โ”€ app.py
โ”œโ”€โ”€ requirements.txt
โ””โ”€โ”€ README.md
Enter fullscreen mode Exit fullscreen mode

My app.py had two routes:

  • /: returns a simple greeting
  • /db: connects to PostgreSQL and returns the DB version

The goal? Run this app in a Docker container and use Docker Compose to spin up both the app and a PostgreSQL service in one command.


๐Ÿณ Dockerfile Setup

I created a simple Dockerfile:

FROM python:3.11-slim

WORKDIR /app

COPY requirements.txt .
RUN pip install -r requirements.txt

COPY . .

CMD ["python", "app.py"]
Enter fullscreen mode Exit fullscreen mode

Looks good, right? ๐Ÿค“ But hereโ€™s where the trouble startedโ€ฆ


๐Ÿ’ฅ The First Crash: Dependency Drama

When I ran:

docker compose -f compose.yml up --build
Enter fullscreen mode Exit fullscreen mode

Boom ๐Ÿ’ฃ

ERROR: Could not find a version that satisfies the requirement click==8.2.0
Enter fullscreen mode Exit fullscreen mode

Hmm. Turns out the click==8.2.0 version in requirements.txt requires Python >= 3.10, but I was using python:3.9-slim-buster as my base image.

๐Ÿ”ง Fix:

I upgraded the base image in my Dockerfile to:

FROM python:3.11-slim
Enter fullscreen mode Exit fullscreen mode

Problem solved. โœ…


๐ŸงŠ The Second Freeze: Compose Conundrum

Feeling confident, I re-ran the build and... ๐Ÿ’ฅ another error:

KeyError: 'ContainerConfig'
Enter fullscreen mode Exit fullscreen mode

At this point, Iโ€™m like, โ€œWhat even is ContainerConfig?โ€ ๐Ÿ˜ค

๐Ÿ•ต๏ธโ€โ™‚๏ธ Root Cause:

I was using Docker Compose v1.29.2 (legacy), which doesn't play nice with newer images or Docker Engine versions.

๐Ÿ”ง Fix:

I upgraded to Docker Compose v2 using:

mkdir -p ~/.docker/cli-plugins/
curl -SL https://github.com/docker/compose/releases/latest/download/docker-compose-linux-x86_64 \
  -o ~/.docker/cli-plugins/docker-compose
chmod +x ~/.docker/cli-plugins/docker-compose
Enter fullscreen mode Exit fullscreen mode

And voilร ! docker compose up started working without the dreaded ContainerConfig error. ๐ŸŽ‰


๐ŸŸข Final Compose File

Hereโ€™s a sneak peek at my compose.yml:

version: '3.8'

services:
  app:
    build: .
    ports:
      - "5000:5000"
    environment:
      - DATABASE_URL=postgresql://postgres:mysecretpassword@db:5432/mydatabase
    depends_on:
      - db

  db:
    image: postgres:13
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: mysecretpassword
      POSTGRES_DB: mydatabase
    volumes:
      - db-data:/var/lib/postgresql/data

volumes:
  db-data:
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ The Sweet Success

After all the bumps, running the final command:

docker compose up --build
Enter fullscreen mode Exit fullscreen mode

Image description

โ€ฆworked flawlessly. I visited:

Image description

  • http://localhost:5000/ โ†’ Hello from Flask โœ…

Image description

  • http://localhost:5000/db โ†’ PostgreSQL version info โœ…

๐Ÿ’ก Key Takeaways

  • ๐Ÿ Match your Python version with compatible libraries (watch those requirements.txt entries)
  • ๐Ÿณ Use Docker Compose v2 for smoother experience with modern images
  • ๐Ÿง  Donโ€™t panic when you hit errorsโ€”each one teaches you something
  • ๐Ÿ”ง Always check if errors are due to version mismatches, especially with Docker

๐Ÿ Final Words

This small project taught me that deployment isn't always plug-and-playโ€”but that's what makes it rewarding. If you're running into Docker or Python-related issues, you're not alone. Just breathe, dig deep, and Google like a ninja. ๐Ÿ’ป


Want more stories like this or need help with your own cloud projects? Follow me at CloudWithHorla โ˜๏ธ๐Ÿ’ช

Top comments (0)