Why and what
Compose describes services, networks and volumes in YAML. It is useful for repeatable development and small deployments; it does not by itself provide multi-host scheduling or high availability. Service names become DNS names on the Compose network.
Lab configuration
Create a .env file with strong lab-only MYSQL_PASSWORD and MYSQL_ROOT_PASSWORD values, restrict it to mode 600 and add it to .gitignore. Use this Compose file with an existing static-site image from the previous chapter:
services:
web:
image: academy-web:1
ports:
- '127.0.0.1:8080:80'
restart: unless-stopped
db:
image: mysql:8.4
environment:
MYSQL_DATABASE: academy
MYSQL_USER: academy
MYSQL_PASSWORD: ${MYSQL_PASSWORD:?Set MYSQL_PASSWORD}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:?Set MYSQL_ROOT_PASSWORD}
volumes:
- mysql_data:/var/lib/mysql
restart: unless-stopped
volumes:
mysql_data:This lab intentionally demonstrates two services; the static web image does not query MySQL. In a dynamic application, configure its database hostname as db, and add startup retry/readiness handling. Do not pretend that merely defining both services wires application code to the database.
Operate
docker compose config --quiet
docker compose up -d
docker compose ps
docker compose logs --tail 30 db
docker compose exec db mysql -u academy -p academy
docker compose downdown removes containers/networks while leaving named volumes by default. down -v removes declared named volumes and can erase the database. Environment variables can be inspected by privileged users; production secret handling should be stronger than a plain env file.
Readiness and failure
Start order is not application readiness. A database can have a running process while initialization is incomplete. Use health checks and retry logic with clear failure messages. Inspect container logs and verify persisted rows after recreating a container.
Assignment
Create a table and row, stop/recreate the stack without deleting volumes and confirm the row remains. Then describe a backup plan that survives host loss; a volume on the same host is persistence, not off-host disaster recovery.
Official reference
Ravindra’s Tip
Container के अंदर localhost उसी container को बताता है। दूसरे service के लिए उसका Compose service name इस्तेमाल करो।
Interview and revision check
Why is down -v different from down?
The volume-removal option can delete persisted database storage. Review exactly which volumes belong to the stack first.
Ravindra Bagale · Cloud & DevOps Academy · Handbook and project downloads