متقدم
البحث فيالمعامل
طريقة البحث
  • ابحث عن مهمة أو أداة (lvm، selinux، ‎@podman)؛ وصفِّ حسب المستوى والكتاب أو الدورة.
  • مع مربع بحث فارغ، الفلتر وحده يعرض المعامل — مثلًا كل المعامل الصعبة لدورة واحدة.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

معمل 8.1الحاويات, الفصل 8

Run a web app, a database and a cache with Docker Compose

المستوى
متوسط
45 دقيقة
الأجهزة
Docker with the Compose plugin (docker compose version)
الحالة
لم يبدأ

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

الهدف

Build a three-service stack the way you would run it on a small server: a web app, PostgreSQL with a named volume and a health check, and Redis on a private network that the internet can not reach. The web app starts only when the database is healthy, secrets come from a file, and a second Compose file changes the stack for development.

قبل أن تبدأ

المهام

  1. Write compose.yaml with the services web (traefik/whoami, port 8080 on the host → 80), db (postgres:16-alpine) and cache (redis:7-alpine).
  2. Use two networks: front (web) and back (web, db, cache). Mark back as internal: true.
  3. Give db a named volume pgdata, the password from a Compose secret (POSTGRES_PASSWORD_FILE), and a health check with pg_isready.
  4. Make web wait with depends_on → condition: service_healthy.
  5. Set limits: db 512 MB memory, cache 128 MB and maxmemory 100 MB; all services restart: unless-stopped.
  6. Start the stack, check the health, read the logs of one service, and open a psql shell in db.
  7. Write compose.override.yaml that publishes Postgres on 127.0.0.1:5432 for development only. Show the merged config.
  8. Stop the stack, start it again and prove the data in the volume survived. Then remove everything including the volume.

تحقّق من عملك

docker compose ps
curl -s localhost:8080 | head -3
docker compose exec db psql -U postgres -c "select count(*) from notes;"
docker compose config --services
docker network inspect compose-lab_back --format '{{.Internal}}'
  • docker compose ps shows 3 services; db is (healthy).
  • curl shows Hostname: from the whoami container.
  • After down and up (without -v) the table notes still has your row.
  • The back network prints true (no internet route).

تلميحات

اعرض التلميحات
  • A secret: top-level secrets: db_password: file: ./db_password.txt and in the service secrets: [db_password]; the file appears at /run/secrets/db_password.
  • Health check: test: ["CMD-SHELL", "pg_isready -U postgres"], interval: 5s, retries: 10.
  • docker compose up -d --wait waits until all health checks pass.
  • docker compose reads compose.yaml and compose.override.yaml automatically; docker compose -f compose.yaml up ignores the override (production).

الحل

اعرض الحل
  1. The main file.
    compose.yaml
    name: compose-lab
    services:
      web:
        image: traefik/whoami:v1.10
        ports: ["8080:80"]
        networks: [front, back]
        depends_on:
          db:
            condition: service_healthy
          cache:
            condition: service_started
        restart: unless-stopped
     
      db:
        image: postgres:16-alpine
        environment:
          POSTGRES_PASSWORD_FILE: /run/secrets/db_password
        secrets: [db_password]
        volumes: [pgdata:/var/lib/postgresql/data]
        networks: [back]
        healthcheck:
          test: ["CMD-SHELL", "pg_isready -U postgres"]
          interval: 5s
          timeout: 3s
          retries: 10
        deploy:
          resources:
            limits:
              memory: 512M
        restart: unless-stopped
     
      cache:
        image: redis:7-alpine
        command: ["redis-server", "--maxmemory", "100mb", "--maxmemory-policy", "allkeys-lru"]
        networks: [back]
        deploy:
          resources:
            limits:
              memory: 128M
        restart: unless-stopped
     
    networks:
      front: {}
      back:
        internal: true
     
    volumes:
      pgdata: {}
     
    secrets:
      db_password:
        file: ./db_password.txt
  2. Start and check.
    docker compose up -d --wait
    docker compose ps
    docker compose logs --tail 20 db
    docker compose exec db psql -U postgres -c "create table notes(t text); insert into notes values ('hello');"
  3. The development override.
    compose.override.yaml
    services:
      db:
        ports: ["127.0.0.1:5432:5432"]
        networks: [back, front]
    docker compose config | grep -A3 ports
    docker compose up -d
  4. Data survives a restart; then clean up.
    docker compose down
    docker compose up -d --wait
    docker compose exec db psql -U postgres -c "select count(*) from notes;"
    docker compose down -v

    down keeps named volumes; down -v deletes them too.

ملاحظاتي

لا توجد ملاحظات بعد.

دروس هذا المعمل

الأوامر في هذا المعمل