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
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"]
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
Boom ๐ฃ
ERROR: Could not find a version that satisfies the requirement click==8.2.0
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
Problem solved. โ
๐ง The Second Freeze: Compose Conundrum
Feeling confident, I re-ran the build and... ๐ฅ another error:
KeyError: 'ContainerConfig'
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
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:
๐ฏ The Sweet Success
After all the bumps, running the final command:
docker compose up --build
โฆworked flawlessly. I visited:
-
http://localhost:5000/โ Hello from Flask โ
-
http://localhost:5000/dbโ PostgreSQL version info โ
๐ก Key Takeaways
- ๐ Match your Python version with compatible libraries (watch those
requirements.txtentries) - ๐ณ 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)