-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker_demo.py
More file actions
663 lines (521 loc) · 17.3 KB
/
Copy pathdocker_demo.py
File metadata and controls
663 lines (521 loc) · 17.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
"""
docker_fastapi_demo.py
Comprehensive demonstration of Dockerizing a Python FastAPI project.
Docker is a platform for developing, shipping, and running applications in containers.
Containers are lightweight and contain everything needed to run the software including
the runtime, libraries, and dependencies.
Why Docker for Python Projects?
- Reproducible environments across different machines
- Easy deployment to production
- Isolation from system dependencies
- Consistent development workflow
- Scalability
Why FastAPI?
- Modern, high-performance Python web framework
- Automatic API documentation (Swagger UI, ReDoc)
- Built-in data validation with Pydantic
- Async/await support out of the box
- Type hints support
Key Topics Covered:
- Understanding Docker images and containers
- Creating a Dockerfile for FastAPI applications
- Best practices for Python Docker images
- Using docker-compose for multi-container applications
- Building, running, and managing containers
- Production deployment with Gunicorn and Uvicorn
This demo includes a FastAPI application that demonstrates:
- Basic FastAPI application structure
- Environment variable configuration
- Pydantic models for data validation
- Automatic API documentation
- Health checks
- Multi-stage builds for production optimization
"""
import subprocess
import os
import sys
# ============================================================================
# SIMPLE FASTAPI APPLICATION (The app we'll Dockerize)
# ============================================================================
# This is the application code that will run inside the Docker container.
# In a real project, this would be in a separate file like main.py
APP_CODE = '''
"""
FastAPI application for Docker demonstration.
FastAPI is a modern, fast (high-performance), web framework for building APIs
with Python 3.7+ based on standard Python type hints.
"""
from fastapi import FastAPI, HTTPException
from fastapi.responses import JSONResponse
from pydantic import BaseModel, Field
from typing import List, Optional
import os
import uvicorn
# Configuration from environment variables
APP_NAME = os.environ.get("APP_NAME", "FastAPIDockerDemo")
APP_VERSION = os.environ.get("APP_VERSION", "1.0.0")
DEBUG_MODE = os.environ.get("DEBUG_MODE", "false").lower() == "true"
# Create FastAPI application
app = FastAPI(
title=APP_NAME,
version=APP_VERSION,
description="A demonstration FastAPI application containerized with Docker",
debug=DEBUG_MODE
)
# Pydantic models for data validation
class Item(BaseModel):
"""Model for an item in our data store."""
id: int
name: str = Field(..., min_length=1, max_length=100)
description: Optional[str] = None
price: float = Field(..., gt=0)
tax: Optional[float] = None
class ItemCreate(BaseModel):
"""Model for creating a new item."""
name: str = Field(..., min_length=1, max_length=100)
description: Optional[str] = None
price: float = Field(..., gt=0)
tax: Optional[float] = None
class HealthResponse(BaseModel):
"""Model for health check response."""
status: str
app: str
class AppInfo(BaseModel):
"""Model for application info response."""
message: str
version: str
debug: bool
# In-memory data store (replace with database in production)
items_db: dict[int, Item] = {}
@app.get("/", response_model=AppInfo)
async def root():
"""Root endpoint returning basic app info."""
return AppInfo(
message=f"Welcome to {APP_NAME}!",
version=APP_VERSION,
debug=DEBUG_MODE
)
@app.get("/health", response_model=HealthResponse)
async def health_check():
"""Health check endpoint for container orchestration."""
return HealthResponse(
status="healthy",
app=APP_NAME
)
@app.get("/api/items", response_model=List[Item])
async def get_items(skip: int = 0, limit: int = 100):
"""Get all items with pagination."""
items = list(items_db.values())
return items[skip : skip + limit]
@app.get("/api/items/{item_id}", response_model=Item)
async def get_item(item_id: int):
"""Get a specific item by ID."""
if item_id not in items_db:
raise HTTPException(status_code=404, detail="Item not found")
return items_db[item_id]
@app.post("/api/items", response_model=Item, status_code=201)
async def create_item(item: ItemCreate):
"""Create a new item."""
# Generate new ID
new_id = max(items_db.keys(), default=0) + 1
# Create full item
new_item = Item(
id=new_id,
name=item.name,
description=item.description,
price=item.price,
tax=item.tax
)
items_db[new_id] = new_item
return new_item
@app.put("/api/items/{item_id}", response_model=Item)
async def update_item(item_id: int, item: ItemCreate):
"""Update an existing item."""
if item_id not in items_db:
raise HTTPException(status_code=404, detail="Item not found")
updated_item = Item(
id=item_id,
name=item.name,
description=item.description,
price=item.price,
tax=item.tax
)
items_db[item_id] = updated_item
return updated_item
@app.delete("/api/items/{item_id}")
async def delete_item(item_id: int):
"""Delete an item."""
if item_id not in items_db:
raise HTTPException(status_code=404, detail="Item not found")
del items_db[item_id]
return {"message": "Item deleted successfully"}
if __name__ == "__main__":
port = int(os.environ.get("PORT", 8000))
uvicorn.run(
"main:app",
host="0.0.0.0",
port=port,
reload=DEBUG_MODE,
log_level="info"
)
'''
# ============================================================================
# DOCKERFILE - The recipe for building our Docker image
# ============================================================================
DOCKERFILE = '''# Use official Python runtime as base image
# We use a specific tag for reproducible builds
FROM python:3.11-slim
# Set working directory
WORKDIR /app
# Set environment variables
# PYTHONDONTWRITEBYTECODE prevents Python from creating .pyc files
# PYTHONUNBUFFERED ensures output is streamed immediately
# UVICORN_WEB_CONCURRENCY optimizes worker count
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
PORT=8000 \
UVICORN_WEB_CONCURRENCY=4
# Install system dependencies required for some Python packages
RUN apt-get update && apt-get install -y --no-install-recommends \\
gcc \\
&& rm -rf /var/lib/apt/lists/*
# Copy requirements file first (for better Docker layer caching)
COPY requirements.txt .
# Install Python dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Copy application code
COPY main.py .
# Expose the port the app runs on
EXPOSE 8000
# Define health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \\
CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/health')" || exit 1
# Run the application with Uvicorn
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
'''
# ============================================================================
# REQUIREMENTS.TXT - Python dependencies
# ============================================================================
REQUIREMENTS_TXT = '''fastapi==0.109.0
uvicorn[standard]==0.27.0
gunicorn==21.2.0
pydantic==2.5.3
pydantic-settings==2.1.0
'''
# ============================================================================
# DOCKER-COMPOSE.YML - For running multi-container applications
# ============================================================================
DOCKER_COMPOSE = '''version: '3.8'
services:
api:
build: .
ports:
- "8000:8000"
environment:
- APP_NAME=FastAPIDocker
- APP_VERSION=1.0.0
- DEBUG_MODE=false
volumes:
- ./main.py:/app/main.py:ro
restart: unless-stopped
healthcheck:
test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8000/health')"]
interval: 30s
timeout: 3s
retries: 3
# Example of adding a PostgreSQL database
postgres:
image: postgres:15-alpine
ports:
- "5432:5432"
environment:
- POSTGRES_USER=fastapi
- POSTGRES_PASSWORD=fastapi
- POSTGRES_DB=fastapi_db
volumes:
- postgres_data:/var/lib/postgresql/data
restart: unless-stopped
# Example of adding Redis for caching
redis:
image: redis:7-alpine
ports:
- "6379:6379"
volumes:
- redis_data:/data
restart: unless-stopped
volumes:
postgres_data:
redis_data:
'''
# ============================================================================
# MULTI-STAGE DOCKERFILE - For production-optimized images
# ============================================================================
DOCKERFILE_MULTISTAGE = '''# Stage 1: Build stage
FROM python:3.11-slim as builder
WORKDIR /app
# Install build dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \\
gcc \\
&& rm -rf /var/lib/apt/lists/*
# Install Python build tools
RUN pip install --no-cache-dir build
# Copy and install dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Stage 2: Production stage
FROM python:3.11-slim
WORKDIR /app
# Install production dependencies only
RUN pip install --no-cache-dir -r requirements.txt
# Create non-root user for security
RUN groupadd -r appgroup && useradd -r -g appgroup appuser
# Copy application code
COPY --chown=appuser:appgroup main.py .
# Switch to non-root user
USER appuser
# Expose port
EXPOSE 8000
# Run with Gunicorn + Uvicorn workers for production
# Gunicorn handles worker management, Uvicorn runs ASGI workers
CMD ["gunicorn", "main:app", "-k", "uvicorn.workers.UvicornWorker", "--bind", "0.0.0.0:8000", "--workers", "4"]
'''
# ============================================================================
# DOCKER COMMANDS AND USAGE
# ============================================================================
def print_dockerfile_sample():
"""Display the Dockerfile content."""
print("=" * 70)
print("DOCKERFILE - Recipe for building the Docker image")
print("=" * 70)
print(DOCKERFILE)
def print_requirements_sample():
"""Display the requirements.txt content."""
print("\n" + "=" * 70)
print("REQUIREMENTS.TXT - Python dependencies")
print("=" * 70)
print(REQUIREMENTS_TXT)
def print_docker_compose_sample():
"""Display the docker-compose.yml content."""
print("\n" + "=" * 70)
print("DOCKER-COMPOSE.YML - Multi-container configuration")
print("=" * 70)
print(DOCKER_COMPOSE)
def print_multistage_dockerfile():
"""Display the multi-stage Dockerfile."""
print("\n" + "=" * 70)
print("MULTI-STAGE DOCKERFILE - Production optimized")
print("=" * 70)
print(DOCKERFILE_MULTISTAGE)
def print_docker_commands():
"""Display common Docker commands."""
print("\n" + "=" * 70)
print("COMMON DOCKER COMMANDS")
print("=" * 70)
commands = """
# Build the Docker image
docker build -t my-fastapi-app .
# Run the container
docker run -p 8000:8000 my-fastapi-app
# Run in detached mode (background)
docker run -d -p 8000:8000 --name my-api my-fastapi-app
# View running containers
docker ps
# View all containers (including stopped)
docker ps -a
# Stop a container
docker stop my-api
# Remove a container
docker remove my-api
# View logs
docker logs my-api
# Follow logs in real-time
docker logs -f my-api
# Interactive shell inside container
docker exec -it my-api /bin/bash
# Using docker-compose
docker-compose up -d # Build and run
docker-compose down # Stop and remove
docker-compose build # Rebuild
docker-compose logs -f # Follow logs
# Access FastAPI docs (automatically generated)
# Once running, visit:
# - Swagger UI: http://localhost:8000/docs
# - ReDoc: http://localhost:8000/redoc
# - OpenAPI JSON: http://localhost:8000/openapi.json
"""
print(commands)
def print_best_practices():
"""Display Docker best practices for Python/FastAPI."""
print("\n" + "=" * 70)
print("DOCKER BEST PRACTICES FOR FASTAPI")
print("=" * 70)
practices = """
1. USE SPECIFIC PYTHON VERSIONS
- Use python:3.11-slim instead of python:latest
- This ensures reproducible builds
- Consider python:3.11-alpine for even smaller images
2. USE SMALL BASE IMAGES
- Prefer slim or alpine variants
- Example: python:3.11-slim instead of python:3.11
- Alpine images are smaller but may have compatibility issues
3. LAYER CACHING OPTIMIZATION
- Copy requirements.txt first, then install
- Copy application code last
- This caches dependency installation
4. USE NON-ROOT USERS
- Create and use a non-root user for security
- Prevents privilege escalation attacks
- Add USER directive at the end of Dockerfile
5. MINIMIZE LAYERS
- Combine RUN commands when possible
- Remove unnecessary files in same layer
6. USE MULTI-STAGE BUILDS
- Build in one stage, run in another
- Reduces final image size significantly
- Excludes build dependencies from production image
7. ADD HEALTH CHECKS
- FastAPI has /health endpoint built-in
- Helps orchestration systems monitor your app
- Used by Kubernetes, Docker Compose
8. USE ENVIRONMENT VARIABLES
- Configuration without rebuilding image
- Use pydantic-settings for type-safe config
- Secrets should use Docker secrets or env files
9. INCLUDE .DOCKERIGNORE
- Exclude unnecessary files from build context
- Example: __pycache__, .git, *.pyc, venv/
10. USE GUNICORN WITH UVICORN FOR PRODUCTION
- Uvicorn's dev server is not for production
- Gunicorn provides worker processes
- Use UvicornWorker for ASGI support
"""
print(practices)
def print_dockerignore_sample():
"""Display .dockerignore content."""
print("\n" + "=" * 70)
print(".DOCKERIGNORE - Exclude files from build context")
print("=" * 70)
dockerignore = """
# Git
.git
.gitignore
# Python
__pycache__
*.py[cod]
*$py.class
*.so
.Python
venv/
ENV/
.venv
# Testing
.pytest_cache
.coverage
htmlcov/
# IDEs
.vscode/
.idea/
*.swp
*.swo
# Documentation
*.md
docs/
# Docker
Dockerfile
docker-compose.yml
.dockerignore
# CI/CD
.github/
.gitlab-ci.yml
"""
print(dockerignore)
# ============================================================================
# MAIN DEMONSTRATION
# ============================================================================
def demonstrate_dockerization():
"""Main demonstration of Dockerizing a FastAPI project."""
print("\n" + "=" * 70)
print("DOCKERIZING A FASTAPI PROJECT - Complete Guide")
print("=" * 70)
print("""
This demonstration shows how to containerize a FastAPI application
using Docker. We'll cover the essential files and best practices.
The sample application is a REST API that demonstrates:
- CRUD operations with Pydantic models
- Automatic API documentation
- Health check endpoint
- Environment variable configuration
- Async/await support
FastAPI Advantages:
- Automatic Swagger UI at /docs
- Automatic ReDoc documentation at /redoc
- Built-in data validation with Pydantic
- Native async support for high performance
- OpenAPI schema generation
Let's walk through each component...
""")
# Show the sample FastAPI application
print("\n" + "=" * 70)
print("SAMPLE FASTAPI APPLICATION (main.py)")
print("=" * 70)
print(APP_CODE)
# Show Dockerfile
print_dockerfile_sample()
# Show requirements.txt
print_requirements_sample()
# Show .dockerignore
print_dockerignore_sample()
# Show docker-compose
print_docker_compose_sample()
# Show multi-stage Dockerfile
print_multistage_dockerfile()
# Show commands
print_docker_commands()
# Show best practices
print_best_practices()
print("\n" + "=" * 70)
print("QUICK START - How to Dockerize Your FastAPI Project")
print("=" * 70)
print("""
Step 1: Create requirements.txt
pip freeze > requirements.txt
Step 2: Create your FastAPI application (main.py)
Write your FastAPI application with Pydantic models
Step 3: Create Dockerfile
Copy the Dockerfile template from this demo
Step 4: Create .dockerignore
Exclude unnecessary files from build
Step 5: Build the image
docker build -t my-fastapi-app .
Step 6: Run the container
docker run -p 8000:8000 my-fastapi-app
Step 7: Access the API
- API: http://localhost:8000
- Swagger Docs: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
- Health Check: http://localhost:8000/health
""")
print("\n" + "=" * 70)
print("DEMONSTRATION COMPLETE")
print("=" * 70)
print("""
You now have all the information needed to Dockerize a FastAPI project.
The key files needed are:
1. main.py - Your FastAPI application
2. requirements.txt - Python dependencies
3. Dockerfile - Image build instructions
4. .dockerignore - Files to exclude
For production, consider:
- Using docker-compose for multi-container apps (database, cache)
- Multi-stage builds for smaller images
- Non-root users for security
- Health checks for monitoring
- Gunicorn with Uvicorn workers for production
- PostgreSQL or other databases for persistence
- Redis for caching
FastAPI automatically generates interactive API documentation:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
""")
if __name__ == "__main__":
demonstrate_dockerization()