Project 02 - Python Flask API + Redis Cache

A REST API built with Python Flask that uses Redis as a caching layer. Demonstrates a very common production pattern - cache expensive operations in Redis to avoid hitting the database every time.

What You Will Learn

  • Writing a Dockerfile for a Python application
  • The caching pattern: check cache first, fallback to data source
  • depends_on between services
  • Named volumes for Redis persistence
  • Using Alpine-based images to keep image size small

Architecture

graph LR Client[" Client\nport 5000"] --> API subgraph DOCKER["Docker Network: flask-network"] API[" flask-app\nPython Flask\nport 5000"] REDIS[(" redis\nRedis 7\nport 6379\nCache")] API -->|"1. Check cache"| REDIS REDIS -->|"2a. Cache HIT -> return instantly"| API REDIS -->|"2b. Cache MISS -> compute + store"| API end

Project Structure

02. Python Flask + Redis/
 app/
    app.py            <- Flask application
    requirements.txt
 docker-compose.yml
 Dockerfile
 README.md

How to Run

cd "Docker Projects/02. Python Flask + Redis"

# Start services
docker compose up -d

# Check they are running
docker compose ps

# Test the API
curl http://localhost:5000/
curl http://localhost:5000/api/visits      # increments visit counter (cached in Redis)
curl http://localhost:5000/api/data/users  # cached endpoint

# Watch cache hits in logs
docker compose logs -f flask-app

# Connect to Redis directly and inspect keys
docker compose exec redis redis-cli
# inside redis-cli:
# KEYS *
# GET visits
# TTL cache:users

# Stop
docker compose down

Key Concepts Demonstrated

ConceptWhere
Caching pattern (check cache -> miss -> store)app/app.py
TTL (Time To Live) on cache keysapp/app.py -> cache_get/set
Alpine-based Python imageDockerfile
Non-root user in containerDockerfile
Redis named volumedocker-compose.yml

FAQ

Should I memorize every Docker command?+

No. Memorize the core workflow first: build, run, list, inspect, logs, exec, stop, remove, and clean up. Then learn specialized commands when you need them.

Is Docker only for developers?+

No. Docker is useful for system administrators, infrastructure engineers, DevOps engineers, cloud engineers, support engineers, and learners who want repeatable labs.

What should I do after reading this guide?+

Run the examples, write down what each command changes, rebuild the workflow with Docker Compose, and then add one CI/CD step that builds the image automatically.

Need help applying Docker in a real project?

Work directly with Muhammad Irfan Aslam for Docker, Linux, DevOps, CI/CD, cloud deployment, or infrastructure troubleshooting support.

Hire Me for Support