Docker Compose — File Basics Exercises

2 minute read

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:

  1. ports was indented too far (under image instead of web)
  2. Port mapping 80:80 needs quotes to avoid YAML interpretation
  3. Volume list item was not indented under volumes:
  4. DEBUG:true missing 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:

  1. Define a container running PostgreSQL
  2. Create a persistent storage location called pgdata
  3. Isolate backend services from frontend services
  4. Store the database password securely
  5. Mount a configuration file for Nginx
Show Answer
  1. services: — Container definitions (e.g., services: db: image: postgres)
  2. volumes: — Named volumes (e.g., volumes: pgdata:)
  3. networks: — Network isolation (e.g., networks: backend: frontend:)
  4. secrets: — Sensitive data (e.g., secrets: db-pass: file: ./pass.txt)
  5. 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?

  1. Start all services in the background
  2. Start services and rebuild any images that have changed
  3. Stop everything and remove all named volumes
  4. Stop everything and remove all images too
  5. Start only the db service 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:

  1. Create a directory called nginx-test
  2. Write a compose.yml running nginx:alpine on port 9090
  3. Create an html/index.html with “Hello Compose”
  4. Mount the html directory as read-only
  5. 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