Docker Compose – Multi Container Applications
Docker Compose – Multi Container Applications
When building real applications, you need:
-
Django
-
PostgreSQL
-
Redis
-
Celery
Managing multiple containers manually is hard.
Enter Docker Compose
Docker Compose allows defining multi-container applications using YAML.
Example docker-compose.yml
version: "3.9"services:web:build: .command: gunicorn project.wsgi:application --bind 0.0.0.0:8000volumes:- .:/codeports:- "8000:8000"depends_on:- dbdb:image: postgres:15environment:POSTGRES_DB: mydbPOSTGRES_USER: myuserPOSTGRES_PASSWORD: mypassword
Run Compose
docker-compose up --buildDocker Volumes & Networks
Volumes
Used for:
Persisting database data
Sharing files
docker volume create myvolumeNetworks
Docker automatically creates bridge networks, but you can create custom ones:
docker network create mynetworkDocker for Production (Best Practices)
Security Best Practices
Use official base images
Use specific version tags
Avoid running as root
Use .dockerignore
Keep images small
Multi-Stage Builds
FROM python:3.12 as builderWORKDIR /appCOPY requirements.txt .RUN pip install --user -r requirements.txtFROM python:3.12-slimWORKDIR /appCOPY --from=builder /root/.local /root/.localCOPY . .CMD ["python", "app.py"]Docker + CI/CD
Docker works perfectly with:
GitHub Actions
GitLab CI/CD
Jenkins
Example flow:
Push code
Build Docker image
Push to Docker Hub
Deploy to server
Docker in Cloud Deployment
Docker can be deployed to:
Amazon ECS
Kubernetes
Google Kubernetes Engine
Azure Kubernetes Service
Comments
Post a Comment