Docker Compose — Commands — Practice Exercises
Practice the essential Docker Compose commands.
Exercise 1: Full Lifecycle
Start a stack, verify it’s running, view logs, then tear it down completely.
Solution
services:
web:
image: nginx:alpine
ports:
- "80:80"
docker compose up -d # Start
docker compose ps # Verify running
docker compose logs web # View logs
docker compose down # Tear down
docker compose ps -a # Verify removed
Exercise 2: Debug a Service
Shell into a running container, check its environment, and test DNS.
Solution
docker compose up -d
docker compose exec web sh
# Inside container:
env # View environment
ping api # Test DNS (if api exists)
cat /etc/nginx/nginx.conf # View config
exit
Exercise 3: Build and Rebuild
Build a service, make a change, then rebuild without cache.
Solution
docker compose build api # Initial build
# Make code changes...
docker compose build --no-cache api # Force full rebuild
docker compose up -d # Start with new image
Exercise 4: Config Validation
Introduce an error in your compose file and use config to find it.
Solution
# Add a typo to compose.yml, then:
docker compose config -q
# Shows: yaml: line X: ...
# Fix the error, then:
docker compose config -q
# No output = valid!
Exercise 5: File Copy
Copy a config file into a running container and verify it.
Solution
echo "test config" > test.conf
docker compose up -d
docker compose cp test.conf web:/etc/nginx/test.conf
docker compose exec web cat /etc/nginx/test.conf
Output: test config