Skip to content

Riju007/docker-hands-on

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

43 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Docker Python Django FastAPI Rust Node.js GitHub Actions


πŸš€ A hands-on Docker learning lab β€” from your very first container to multi-language, production-style architectures with CI/CD.


πŸ“¦ Repository Structure

docker-hands-on/
β”‚
β”œβ”€β”€ 🐍 docker-basics/              # Start here! Your first Dockerfile & Python app
β”œβ”€β”€ πŸ¦„ fastapi-demo-01/            # FastAPI + PostgreSQL with Docker Compose
β”œβ”€β”€ 🎸 django-docker-demo/         # Django 6 + PostgreSQL + multi-stage builds + CI/CD
β”œβ”€β”€ 🌐 polyglot-docker/            # Python + Node.js + Rust + Nginx + Grafana/Loki
└── πŸ”¬ docker-debug-lab/           # Simulate real production incidents & debug them

πŸ—ΊοΈ Learning Path

Follow these projects in order for the smoothest learning journey:

β‘  docker-basics       β†’    β‘‘ fastapi-demo-01    β†’    β‘’ django-docker-demo
   First container            Compose + DB               Multi-stage + CI/CD

                                    ↓

β‘£ docker-debug-lab    β†’    β‘€ polyglot-docker
   Debug like a pro             Multi-language microservices + Observability

🐍 1. docker-basics β€” Your First Container

What you'll learn: Writing a Dockerfile, building an image, running a container, reading live logs.

docker-basics/
β”œβ”€β”€ app.py         # Simple Python counter app
└── Dockerfile     # Your first Dockerfile

The App: A Python script that prints a running counter every 2 seconds β€” perfect for watching logs live.

# app.py
while True:
    print(f"Running...{counter}!!", flush=True)
    counter += 1
    time.sleep(2)

The Dockerfile:

FROM python:3.12
WORKDIR /app
COPY . /app/
CMD ["python", "-u", "app.py"]

▢️ Try it:

cd docker-basics
docker build -t my-first-app .
docker run my-first-app
# Watch the counter tick... πŸŽ‰

πŸ¦„ 2. fastapi-demo-01 β€” FastAPI + PostgreSQL

What you'll learn: Docker Compose, multi-container apps, volume persistence, service dependencies.

fastapi-demo-01/
β”œβ”€β”€ main.py              # FastAPI app with health-check endpoint
β”œβ”€β”€ requirements.txt     # fastapi, uvicorn, watchfiles
β”œβ”€β”€ Dockerfile
└── docker-compose.yml   # Web + PostgreSQL services

Architecture:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  FastAPI  :8000 β”‚ ──────► β”‚  PostgreSQL :5432 β”‚
β”‚  (fast-api-     β”‚         β”‚  (fast-api-db)    β”‚
β”‚   demo-01)      β”‚         β”‚                   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Endpoints:

Method Path Description
GET / Hello from Docker!
GET /health-check Returns {"status": "Healthy"}

▢️ Try it:

cd fastapi-demo-01
docker compose up --build

# Test it
curl http://localhost:8000/health-check

🎸 3. django-docker-demo β€” Production-Ready Django

What you'll learn: Multi-stage Docker builds, Django + PostgreSQL, GitHub Actions CI/CD, GHCR image publishing.

django-docker-demo/
β”œβ”€β”€ config/                  # Django project settings, URLs, WSGI/ASGI
β”œβ”€β”€ core/                    # Core app with health-check view
β”‚   └── views.py             # Returns {"status": "Healthy"}
β”œβ”€β”€ requirements.txt         # Django 6, psycopg, etc.
β”œβ”€β”€ Dockerfile               # ✨ Multi-stage build (builder + runtime)
β”œβ”€β”€ docker-compose.yml       # Web + PostgreSQL
└── .github/workflows/
    └── django_docker_demo.yml  # πŸš€ Build & push to GHCR on every push

✨ Multi-Stage Dockerfile Explained

This is where things get serious! The Dockerfile uses two stages to keep the final image lean:

# ── Stage 1: Builder ──────────────────────────────
FROM python:3.12-slim AS builder
WORKDIR /app
RUN apt-get update && apt-get install -y build-essential  # compile deps
COPY requirements.txt .
RUN pip install -r requirements.txt                       # install into /usr/local

# ── Stage 2: Runtime ──────────────────────────────
FROM python:3.12-slim                                     # fresh, clean image
WORKDIR /app
COPY --from=builder /usr/local /usr/local                 # copy ONLY installed packages
COPY . /app/
ENV PYTHONUNBUFFERED=1
EXPOSE 8000
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

πŸ’‘ Why two stages? The builder stage needs build-essential (heavy!) to compile Python packages. The runtime stage copies only the installed packages β€” no build tools, no bloat. Smaller image = faster deploys!

πŸ”„ CI/CD Pipeline (GitHub Actions)

Every push to any branch automatically:

  1. βœ… Checks out the code
  2. 🏷️ Sets a lowercase image name from your GitHub username
  3. πŸ” Logs into GitHub Container Registry (GHCR)
  4. πŸ—οΈ Builds the Docker image (tagged with the commit SHA)
  5. πŸ“€ Pushes the image to ghcr.io/<your-username>/docker-hands-on

▢️ Try it:

cd django-docker-demo
docker compose up --build

# Health check
curl http://localhost:8000/health-check/
# β†’ {"status": "Healthy"}

🌐 4. polyglot-docker β€” Multi-Language Microservices

What you'll learn: Multi-container orchestration across different languages, Nginx reverse proxy, centralized logging with Grafana + Loki + Promtail.

polyglot-docker/
β”œβ”€β”€ python-api/          # FastAPI service (Python 3.12)
β”œβ”€β”€ node-api/            # Express service (Node.js 22)
β”œβ”€β”€ rust-api/            # Axum service (Rust) β€” multi-stage build
β”œβ”€β”€ nginx/
β”‚   └── nginx.conf       # Reverse proxy routing all 3 APIs
β”œβ”€β”€ promtail-config.yaml # Scrapes Docker container logs
└── compose.yaml         # Wires everything together

πŸ—οΈ Architecture

                   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                   β”‚    Nginx  :5000       β”‚
                   β”‚   (reverse proxy)     β”‚
                   β””β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”˜
                       β”‚      β”‚       β”‚
              /api/    β”‚  /node/   /rust/
                       β–Ό      β–Ό       β–Ό
           β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
           β”‚  Python  β”‚ β”‚  Node  β”‚ β”‚   Rust   β”‚
           β”‚  FastAPI β”‚ β”‚Express β”‚ β”‚  Axum    β”‚
           β”‚  :8000   β”‚ β”‚ :3000  β”‚ β”‚  :4000   β”‚
           β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

           β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
           β”‚  Grafana :3001 ◄── Loki :3100       β”‚
           β”‚         ◄── Promtail (log scraper)  β”‚
           β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ”€ Nginx Routing

Path Routes To
/api/ Python FastAPI (:8000)
/node/ Node.js Express (:3000)
/rust/ Rust Axum (:4000)

πŸ¦€ Rust Multi-Stage Build

The Rust API uses a two-stage build β€” compile in rust:1.93, ship in debian:bookworm-slim:

FROM rust:1.93 AS builder
RUN cargo build --release      # heavy compile step

FROM debian:bookworm-slim      # tiny runtime image
COPY --from=builder /app/target/release/rust-api /app
CMD ["./rust-api"]

πŸ“Š Observability Stack

Tool Role Port
Promtail Scrapes Docker container logs β€”
Loki Log aggregation & storage 3100
Grafana Visualize logs & build dashboards 3001

▢️ Try it:

cd polyglot-docker
docker compose up --build

curl http://localhost:5000/api/health      # Python
curl http://localhost:5000/node/health    # Node.js
curl http://localhost:5000/rust/health    # Rust
curl http://localhost:5000/api/aggregate  # Python calls Node + Rust!

# Open Grafana at http://localhost:3001

πŸ”¬ 5. docker-debug-lab β€” Simulate Production Incidents

What you'll learn: Debugging containers using docker logs, docker stats, and docker inspect. Recognize common failure patterns before they bite you in production.

docker-debug-lab/
β”œβ”€β”€ services/
β”‚   β”œβ”€β”€ api-crash/      # Simulates a crash loop
β”‚   β”œβ”€β”€ api-memory/     # Simulates a memory leak
β”‚   β”œβ”€β”€ api-network/    # Simulates network failures
β”‚   └── api-good/       # A healthy baseline service
└── compose.yaml

Incident Simulation Table:

# Incident Container Debug Skill
1 πŸ’₯ Crash loop crash-api docker logs
2 🧠 Memory leak memory-api docker stats
3 🌐 Network failure network-api docker inspect
4 βœ… Healthy baseline good-api Comparison reference

Key debug commands to practice:

docker logs <container>           # What did it print before dying?
docker stats                      # Live CPU/memory per container
docker inspect <container>        # Full config, network, mounts
docker exec -it <container> sh    # Shell in to investigate live

⚑ Quick Reference β€” Docker Cheatsheet

# πŸ—οΈ Build & Run
docker build -t my-app .
docker run -d -p 8000:8000 my-app
docker compose up --build

# πŸ” Inspect & Debug
docker ps                          # running containers
docker ps -a                       # all containers
docker logs -f <container>         # follow logs
docker stats                       # live resource usage
docker exec -it <container> sh     # shell into container
docker inspect <container>         # full container details

# 🧹 Cleanup
docker stop <container>
docker rm <container>
docker rmi <image>
docker system prune -a             # ⚠️ removes everything unused

πŸ› οΈ Full Tech Stack

Technology Used In
🐍 Python 3.12 docker-basics, fastapi-demo-01, django-docker-demo, polyglot-docker
πŸ¦„ FastAPI + Uvicorn fastapi-demo-01, polyglot-docker
🎸 Django 6 django-docker-demo
πŸ¦€ Rust (Axum + Tokio) polyglot-docker
🟩 Node.js 22 (Express) polyglot-docker
🐘 PostgreSQL 16 fastapi-demo-01, django-docker-demo
🌐 Nginx polyglot-docker (reverse proxy)
πŸ“Š Grafana + Loki + Promtail polyglot-docker (observability)
βš™οΈ GitHub Actions + GHCR django-docker-demo (CI/CD)

πŸš€ Getting Started

Prerequisites: Install Docker Desktop

# 1. Clone the repo
git clone https://github.com/Riju007/docker-hands-on.git
cd docker-hands-on

# 2. Start with the basics
cd docker-basics
docker build -t my-first-app .
docker run my-first-app

# 3. Work your way up through each project! 🎯

Made with ❀️ and lots of β˜• by Riju

Learning in public. Failing forward. Shipping containers. 🐳

GitHub Follow

⭐ If this helped you learn Docker, drop a star! ⭐

About

This repository is for learning and experimenting with docker

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors