دليل Docker Compose: إدارة تطبيقات متعددة الحاويات
تعريف وإدارة التطبيقات متعددة الحاويات باستخدام Docker Compose: تبعيات الخدمات، متغيرات البيئة، فحوصات الصحة، التوسع والنشر في بيئة الإنتاج. دليل شامل.
جدول المحتويات
دليل Docker Compose: إدارة تطبيقات متعددة الحاويات
Docker Compose أداة تتيح لك تعريف وإدارة حاويات Docker متعددة باستخدام ملف YAML واحد. وهي ضرورية لمعماريات الخدمات المصغرة وتطبيقات الويب وبيئات التطوير.
ما هو Docker Compose؟
مع Docker Compose يمكنك:
- تعريف خدمات متعددة في ملف
docker-compose.ymlواحد - إدارة التبعيات بين الخدمات
- إدارة تكوينات الشبكة والأحجام بشكل مركزي
- تشغيل/إيقاف المكدس بأكمله بأمر واحد
يعمل Docker Compose v2 بالأمر docker compose (بدون شرطة) وهو مدمج في Docker CLI. لا يزال الأمر القديم docker-compose مدعوماً.
هيكل docker-compose.yml الأساسي
version: '3.9'
services:
web:
image: nginx:alpine
ports:
- "80:80"
volumes:
- ./html:/usr/share/nginx/html:ro
restart: unless-stopped
db:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: كلمة_مرور_سرية
MYSQL_DATABASE: تطبيقي
volumes:
- db_data:/var/lib/mysql
restart: unless-stopped
volumes:
db_data:
الخدمات والأحجام والشبكات
تعريف الخدمات
services:
app:
build:
context: ./app
dockerfile: Dockerfile
image: تطبيقي:latest
container_name: app-container
ports:
- "3000:3000"
environment:
NODE_ENV: production
DB_HOST: database
depends_on:
database:
condition: service_healthy
networks:
- app-network
restart: unless-stopped
deploy:
resources:
limits:
cpus: '0.5'
memory: 512M
تعريف الأحجام
volumes:
# حجم مسمى (تديره Docker)
db_data:
driver: local
# حجم خارجي (تم إنشاؤه مسبقاً)
existing_volume:
external: true
# خيارات مشغل مخصصة
nfs_volume:
driver: local
driver_opts:
type: nfs
o: addr=192.168.1.100,rw
device: ":/nfs/share"
تعريف الشبكات
networks:
# شبكة bridge افتراضية
app-network:
driver: bridge
# مع شبكة فرعية مخصصة
custom-net:
driver: bridge
ipam:
config:
- subnet: 172.20.0.0/16
# شبكة خارجية
existing-net:
external: true
depends_on وفحص الصحة
تحكم في ترتيب بدء الخدمات باستخدام depends_on:
services:
web:
image: nginx:alpine
depends_on:
api:
condition: service_healthy
db:
condition: service_healthy
api:
build: ./api
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
interval: 30s
timeout: 10s
retries: 3
start_period: 40s
depends_on:
db:
condition: service_healthy
db:
image: postgres:15
environment:
POSTGRES_PASSWORD: كلمة_المرور
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 10s
timeout: 5s
retries: 5
depends_on يتحكم فقط في ترتيب البدء. بدون condition: service_healthy لا يمكنك ضمان جاهزية الخدمة.
متغيرات البيئة وenv_file
التعريف المباشر
services:
app:
image: تطبيقي:latest
environment:
- NODE_ENV=production
- PORT=3000
- DB_HOST=database
- DB_PORT=5432
استخدام ملف .env
services:
app:
image: تطبيقي:latest
env_file:
- .env
- .env.production
ملف .env:
# .env
NODE_ENV=production
DB_HOST=database
DB_PORT=5432
DB_NAME=تطبيقي
DB_USER=مستخدم
DB_PASS=كلمة_مرور_سرية
JWT_SECRET=مفتاح_سري_جداً
REDIS_URL=redis://redis:6379
أضف .env إلى .gitignore. لا تقم أبداً بإيداع المعلومات الحساسة في Git.
التوسع (--scale)
يمكنك توسيع الخدمات أفقياً مع Docker Compose:
# تشغيل خدمة api بـ 3 نسخ
docker compose up -d --scale api=3
# تغيير عدد النسخ الجارية
docker compose up -d --scale api=5
# توسيع خدمة محددة
docker compose scale api=3 worker=2
تكوين موازن التحميل للتوسع:
services:
nginx:
image: nginx:alpine
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
depends_on:
- api
api:
build: ./api
# لا تعرف ports - سيوجه nginx
expose:
- "3000"
environment:
NODE_ENV: production
ملفات Override
استخدم ملفات override لبيئات مختلفة:
# docker-compose.override.yml (التطوير)
services:
app:
build:
context: .
target: development
volumes:
- .:/app
- /app/node_modules
environment:
NODE_ENV: development
command: npm run dev
db:
ports:
- "5432:5432" # فتح للخارج في التطوير
# docker-compose.prod.yml (الإنتاج)
services:
app:
image: registry.example.com/تطبيقي:latest
restart: always
deploy:
replicas: 3
resources:
limits:
cpus: '1.0'
memory: 1G
الاستخدام:
# التطوير (يُطبق override تلقائياً)
docker compose up -d
# الإنتاج
docker compose -f docker-compose.yml -f docker-compose.prod.yml up -d
أفضل الممارسات للإنتاج
مثال كامل للإنتاج
version: '3.9'
services:
nginx:
image: nginx:1.25-alpine
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx/conf.d:/etc/nginx/conf.d:ro
- certbot_data:/etc/letsencrypt:ro
depends_on:
- api
restart: always
logging:
driver: "json-file"
options:
max-size: "10m"
max-file: "3"
api:
image: registry.example.com/api:${APP_VERSION:-latest}
env_file: .env.production
depends_on:
db:
condition: service_healthy
redis:
condition: service_healthy
restart: always
healthcheck:
test: ["CMD", "wget", "-qO-", "http://localhost:3000/health"]
interval: 30s
timeout: 10s
retries: 3
deploy:
resources:
limits:
cpus: '1.0'
memory: 512M
db:
image: postgres:15-alpine
env_file: .env.production
volumes:
- postgres_data:/var/lib/postgresql/data
restart: always
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-postgres}"]
interval: 10s
timeout: 5s
retries: 5
redis:
image: redis:7-alpine
command: redis-server --requirepass ${REDIS_PASSWORD} --maxmemory 256mb --maxmemory-policy allkeys-lru
volumes:
- redis_data:/data
restart: always
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 10s
timeout: 5s
retries: 3
volumes:
postgres_data:
redis_data:
certbot_data:
الأوامر الأساسية
# تشغيل جميع الخدمات
docker compose up -d
# تشغيل خدمة محددة
docker compose up -d api
# متابعة السجلات
docker compose logs -f
docker compose logs -f api
# إيقاف الخدمات
docker compose down
# إيقاف الخدمات وحذف الأحجام
docker compose down -v
# إعادة تشغيل خدمة
docker compose restart api
# عرض حالة الخدمات
docker compose ps
# تنفيذ أمر في خدمة
docker compose exec api bash
# إعادة بناء الصور
docker compose build --no-cache
docker compose up -d --build
# التحقق من التكوين
docker compose config
الخلاصة
Docker Compose هو الطريقة الأكثر عملية لإدارة التطبيقات متعددة الحاويات. يوفر بيئة متسقة من التطوير إلى الإنتاج. مع ملفات override يمكنك تكوين بيئات مختلفة بمرونة، ومع healthcheck وdepends_on يمكنك إدارة تبعيات الخدمات بأمان.
مقالات ذات صلة
النشر التلقائي مع GitHub Actions: إعداد CI/CD Pipeline
إنشاء CI/CD pipelines مع GitHub Actions: بناء ونشر Docker، النشر على الخادم عبر SSH، أتمتة الاختبارات، إدارة الأسرار وتحسين سير العمل.
أتمتة الخوادم مع Ansible: البنية التحتية كرمز
تثبيت Ansible، ملف الجرد، كتابة playbook، هيكل الأدوار، المتغيرات، vault المشفر، أوامر ad-hoc وأتمتة تكوين الخادم. دليل خطوة بخطوة.
إعداد خادم ويب Caddy: HTTPS تلقائي وبروكسي عكسي
تثبيت خادم ويب Caddy، تكوين Caddyfile، SSL تلقائي من Let's Encrypt، بروكسي عكسي، استضافة المواقع الثابتة، تكامل PHP-FPM وإعدادات الأداء.