Skip to content

Docker containerization patterns: Flask applications, multi-stage builds, Docker Compose orchestration, and microservices architecture examples

Notifications You must be signed in to change notification settings

TheDataArtisanDev/docker-containerization-patterns

Repository files navigation

Docker Examples & Learning Repository

Docker Python Flask Redis

A comprehensive collection of Docker examples demonstrating containerization concepts from basic single-container applications to advanced multi-stage builds and orchestration with Docker Compose. This repository provides hands-on examples for learning Docker fundamentals and best practices.

πŸ“‹ Table of Contents

🎯 Project Overview

This repository serves as a practical learning platform for Docker containerization, covering:

  • Basic Containerization with single-service applications
  • Multi-container Applications using Docker Compose
  • Production Optimization with multi-stage builds
  • Service Orchestration and networking
  • Real-world Applications (APIs, report generators, caching)
  • Docker Best Practices and optimization techniques

πŸ“ Repository Structure

Docker-Examples/
β”œβ”€β”€ πŸ“‚ docker-basics/                    # Simple Flask app containerization
β”œβ”€β”€ πŸ“‚ dynamic-report-generator/         # CSV report generation API
β”œβ”€β”€ πŸ“‚ flask-redis-app/                 # Multi-service app with Docker Compose
β”œβ”€β”€ πŸ“‚ multistage-pdf-generator/        # Optimized builds with multi-stage
β”œβ”€β”€ πŸ“„ readme-docker.md                 # Docker fundamentals guide
└── πŸ“„ README.md                        # This file

🐳 Docker Examples

Beginner Level

Docker Basics πŸš€

Simple Flask App Containerization

Basic introduction to Docker containerization with a minimal Python Flask application.

Features:

  • βœ… Simple Dockerfile - Basic container setup
  • βœ… Flask Web App - "Hello World" web application
  • βœ… Port Mapping - Exposing application to host
  • βœ… Image Building - Creating custom Docker images

Quick Start:

cd docker-basics
docker build -t hello-docker .
docker run -p 5000:5000 hello-docker

Access: http://localhost:5000


API-Based CSV Report Generation

Demonstrates building a REST API that generates CSV reports from JSON input.

Features:

  • βœ… REST API - POST endpoint for data processing
  • βœ… JSON to CSV - Dynamic report generation
  • βœ… File Downloads - CSV file response handling
  • βœ… Lightweight Container - Optimized Python image

Quick Start:

cd dynamic-report-generator
docker build -t dynamic-report-generator .
docker run -p 5000:5000 dynamic-report-generator

API Usage:

curl -X POST http://localhost:5000/generate-report \
  -H "Content-Type: application/json" \
  -d '[{"name":"John","age":30,"city":"NYC"}]'

Intermediate Level

Multi-Service Application with Docker Compose

Advanced example showing service orchestration with Docker Compose, featuring a Flask web app connected to Redis for caching.

Features:

  • βœ… Docker Compose - Multi-container orchestration
  • βœ… Service Networking - Container-to-container communication
  • βœ… Redis Integration - Caching and data persistence
  • βœ… Dependency Management - Service startup ordering
  • βœ… Volume Mapping - Data persistence across restarts

Quick Start:

cd flask-redis-app
docker-compose up --build

Access: http://localhost:5000 (view counter increments with each refresh)

Services:

  • Flask App: Web application server
  • Redis: In-memory data store for caching

Advanced Level

Production-Optimized Multi-Stage Build

Demonstrates advanced Docker techniques using multi-stage builds to create minimal, production-ready images.

Features:

  • βœ… Multi-Stage Build - Separate build and runtime environments
  • βœ… Image Optimization - Smaller final image size
  • βœ… PDF Generation - Dynamic PDF creation with FPDF
  • βœ… Security Best Practices - Minimal attack surface
  • βœ… Production Patterns - Optimized for deployment

Quick Start:

cd multistage-pdf-generator
docker build -t pdf-generator .
docker run -p 5000:5000 pdf-generator

Build Stages:

  1. Builder Stage: Install dependencies and build artifacts
  2. Runtime Stage: Copy only necessary files for execution

πŸš€ Getting Started

1. Install Docker

Windows/Mac:

  • Download Docker Desktop from docker.com
  • Follow installation instructions for your platform

Linux:

# Ubuntu/Debian
sudo apt update
sudo apt install docker.io docker-compose

# CentOS/RHEL
sudo yum install docker docker-compose

2. Verify Installation

# Check Docker version
docker --version

# Check Docker Compose version
docker-compose --version

# Test Docker installation
docker run hello-world

3. Clone Repository

git clone https://github.com/TheDataArtisanDev/Docker-Examples.git
cd Docker-Examples

4. Run Your First Example

# Start with basic example
cd docker-basics
docker build -t hello-docker .
docker run -p 5000:5000 hello-docker

Visit http://localhost:5000 to see your containerized application!


πŸ“š Learning Path

Fundamentals

  1. Docker Concepts: Read readme-docker.md for theory
  2. Basic Container: Docker Basics example
  3. Docker Commands: Build, run, and manage containers

Intermediate Concepts

  1. API Development: Dynamic Report Generator
  2. Multi-Container Apps: Flask + Redis
  3. Service Networking: Understanding container communication

Advanced Patterns

  1. Build Optimization: Multistage PDF Generator
  2. Production Deployment: Security and performance considerations
  3. Orchestration: Docker Compose and service management

Production Readiness

  1. Image Optimization: Multi-stage builds and layer caching
  2. Security Practices: Minimal base images and user permissions
  3. Monitoring: Health checks and logging strategies

πŸ› οΈ Essential Docker Commands

Image Management

# Build image
docker build -t <image-name> .

# List images
docker images

# Remove image
docker rmi <image-name>

# Pull from registry
docker pull <image-name>

Container Operations

# Run container
docker run -p <host-port>:<container-port> <image-name>

# List running containers
docker ps

# Stop container
docker stop <container-id>

# Remove container
docker rm <container-id>

Docker Compose

# Start services
docker-compose up

# Build and start
docker-compose up --build

# Stop services
docker-compose down

# View logs
docker-compose logs <service-name>

Debugging

# Execute shell in running container
docker exec -it <container-id> /bin/bash

# View container logs
docker logs <container-id>

# Inspect container
docker inspect <container-id>

πŸ”§ Prerequisites

Software Requirements

  • Docker Engine 20.10+
  • Docker Compose 2.0+
  • Git for repository cloning
  • Web Browser for testing applications

System Requirements

  • RAM: 4GB minimum, 8GB recommended
  • Storage: 10GB free space for images
  • OS: Windows 10+, macOS 10.14+, or modern Linux

Optional Tools

# Docker development tools
docker stats        # Resource monitoring
docker system df    # Disk usage
docker system prune # Cleanup unused resources

πŸ’‘ Real-World Applications

Web Development

  • 🌐 Application Containerization: Package web apps with dependencies
  • πŸ”„ Development Environment: Consistent dev environments across teams
  • πŸš€ Deployment: Simplified application deployment

Microservices

  • πŸ—οΈ Service Isolation: Independent service deployment
  • πŸ“‘ API Integration: Container-to-container communication
  • πŸ“Š Load Balancing: Scaling individual services

DevOps & CI/CD

  • βš™οΈ Build Automation: Containerized build environments
  • πŸ§ͺ Testing: Isolated testing environments
  • πŸ“¦ Artifact Management: Version-controlled container images

🀝 Contributing

This repository focuses on Docker education and practical examples. Contributions welcome:

  • πŸ’‘ New Examples: Add more Docker patterns or use cases
  • πŸ› Bug Fixes: Improve existing examples or documentation
  • πŸ“š Documentation: Enhance explanations or add tutorials
  • πŸ”§ Optimizations: Performance improvements or best practices

For major changes, please open an issue first to discuss your ideas.


πŸ”— Useful Links


Happy Containerizing! 🐳

This repository provides a comprehensive journey through Docker containerization. Use these examples to build production-ready containerized applications!

About

Docker containerization patterns: Flask applications, multi-stage builds, Docker Compose orchestration, and microservices architecture examples

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published