Docker Compose — File Basics Exercises
Practice what you’ve learned in the Compose File Basics chapter. These exercises cover YAML syntax, top-level keys, and the up/down workflow.
Exercise 1 — Fix the YAML
Question: This compose file has 4 YAML errors. Find and fix them:
services:
web:
image: nginx:alpine
ports:
- 80:80
volumes:
- ./html:/usr/share/nginx/html
environment:
DEBUG:true
Show Answer
Fixed version:
services:
web:
image: nginx:alpine
ports: # Fix 1: ports indented at same level as image
- "80:80" # Fix 2: port mapping must be quoted
volumes:
- ./html:/usr/share/nginx/html # Fix 3: list item must be indented
environment:
DEBUG: "true" # Fix 4: space after colon, quote boolean
Errors:
portswas indented too far (underimageinstead ofweb)- Port mapping
80:80needs quotes to avoid YAML interpretation - Volume list item was not indented under
volumes: DEBUG:truemissing space after colon, and “true” should be quoted
Exercise 2 — Top-Level Keys
Question: For each compose.yml feature, name the correct top-level key:
- Define a container running PostgreSQL
- Create a persistent storage location called
pgdata - Isolate backend services from frontend services
- Store the database password securely
- Mount a configuration file for Nginx
Show Answer
services:— Container definitions (e.g.,services: db: image: postgres)volumes:— Named volumes (e.g.,volumes: pgdata:)networks:— Network isolation (e.g.,networks: backend: frontend:)secrets:— Sensitive data (e.g.,secrets: db-pass: file: ./pass.txt)configs:— Configuration files (e.g.,configs: nginx-conf: file: ./nginx.conf)
Exercise 3 — Write a Compose File
Task: Write a compose.yml that runs Redis on port 6379 with a named volume called redis-data mounted at /data.
Show Answer
services:
redis:
image: redis:7-alpine
ports:
- "6379:6379"
volumes:
- redis-data:/data
volumes:
redis-data:
Exercise 4 — Up and Down Options
Question: What command would you use for each scenario?
- Start all services in the background
- Start services and rebuild any images that have changed
- Stop everything and remove all named volumes
- Stop everything and remove all images too
- Start only the
dbservice in the background
Show Answer
# 1. Background start
docker compose up -d
# 2. Rebuild and start
docker compose up --build -d
# 3. Stop + remove volumes
docker compose down -v
# 4. Stop + remove images
docker compose down --rmi all
# 5. Start specific service
docker compose up -d db
Exercise 5 — Hands-On Nginx
Task: Complete this hands-on exercise:
- Create a directory called
nginx-test - Write a
compose.ymlrunning nginx:alpine on port 9090 - Create an
html/index.htmlwith “Hello Compose” - Mount the html directory as read-only
- Start the stack, test with curl, then clean up
Show Answer
# 1. Create directory
mkdir -p ~/nginx-test/html && cd ~/nginx-test
# 2 & 4. Create compose.yml with read-only mount
cat > compose.yml << 'EOF'
services:
web:
image: nginx:alpine
ports:
- "9090:80"
volumes:
- ./html:/usr/share/nginx/html:ro
EOF
# 3. Create HTML file
echo "<h1>Hello Compose</h1>" > html/index.html
# 5. Start, test, cleanup
docker compose up -d
curl http://localhost:9090
# Output: <h1>Hello Compose</h1>
docker compose down
cd ~ && rm -rf ~/nginx-test