Docker Compose — Troubleshooting — Practice Exercises
Practice diagnosing and fixing common Docker Compose problems.
Exercise 1: Fix YAML Error
Find and fix the error in this compose file:
services:
api:
image: node:20-alpine
ports:
- "3000:3000"
environment:
NODE_ENV: production
Solution
The ports: list items need proper indentation:
services:
api:
image: node:20-alpine
ports:
- "3000:3000"
environment:
NODE_ENV: production
Use docker compose config -q to validate.
Exercise 2: Port Conflict
You get “address already in use” on port 3000. Diagnose and fix it.
Solution
# Find what's using port 3000
lsof -i :3000
# Option 1: Kill the process
kill $(lsof -t -i :3000)
# Option 2: Use a different port
# Change compose.yml: "3001:3000"
Exercise 3: DNS Resolution Failure
Container web can’t reach api. Diagnose why.
Solution
Check if they share a network:
docker compose exec web nslookup api
If it fails, ensure both services are on the same network:
services:
web:
networks: [app]
api:
networks: [app]
networks:
app:
Exercise 4: Container Keeps Restarting
A service shows “Restarting” in docker compose ps. Diagnose it.
Solution
# Check logs for crash reason
docker compose logs --tail 50 <service>
# Override entrypoint to investigate
docker compose run --rm --entrypoint sh <service>
# Check if a dependency is missing
docker compose ps -a
Exercise 5: Volume Permission Error
A container can’t write to a mounted volume. Fix it.
Solution
# Check ownership
ls -la ./data
# Fix permissions
sudo chown -R 1000:1000 ./data
# Or set user in compose
services:
api:
user: "1000:1000"
volumes:
- ./data:/app/data