هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
الهدف
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.
قبل أن تبدأ
docker compose versionworks.- Make the folder
~/compose-laband a password file:printf 'S3cret-db-pass' > db_password.txt. - The web app is the public image
traefik/whoami(it prints request details); you only practise the plumbing.
المهام
ضع علامة على المهمة عندما تنتهي منها. متصفحك فقط يتذكر ذلك.
- Write
compose.yamlwith the servicesweb(traefik/whoami, port 8080 on the host → 80),db(postgres:16-alpine) andcache(redis:7-alpine). - Use two networks:
front(web) andback(web, db, cache). Markbackasinternal: true. - Give
dba named volumepgdata, the password from a Compose secret (POSTGRES_PASSWORD_FILE), and a health check withpg_isready. - Make
webwait withdepends_on→condition: service_healthy. - Set limits:
db512 MB memory,cache128 MB andmaxmemory100 MB; all servicesrestart: unless-stopped. - Start the stack, check the health, read the logs of one service, and open a
psqlshell indb. - Write
compose.override.yamlthat publishes Postgres on127.0.0.1:5432for development only. Show the merged config. - 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 psshows 3 services;dbis(healthy).curlshowsHostname:from the whoami container.- After
downandup(without-v) the tablenotesstill has your row. - The
backnetwork printstrue(no internet route).
تلميحات
اعرض التلميحات
- A secret: top-level
secrets: db_password: file: ./db_password.txtand in the servicesecrets: [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 --waitwaits until all health checks pass.docker composereadscompose.yamlandcompose.override.yamlautomatically;docker compose -f compose.yaml upignores the override (production).
الحل
اعرض الحل
- 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 - Start and check.
- The development override.compose.override.yaml
services: db: ports: ["127.0.0.1:5432:5432"] networks: [back, front] - 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 -vdownkeeps named volumes;down -vdeletes them too.
ملاحظاتي
لا توجد ملاحظات بعد.