Loading...

Lab 160: Docker Compose

Bring up a minimal multi-service stack with Docker Compose, verify both services are running, validate nginx responds, inspect logs, then tear everything down cleanly and remove the working directory.

containers services troubleshooting

Scenario

Ops ticket: Spin up a small local stack for testing using Docker Compose. Bring up nginx and redis, verify both services are running, check logs, then tear it down.

Operator context

Compose is often the fastest way to reproduce a multi-service environment locally. Evidence is the service status output, a successful HTTP check, and targeted log inspection.

Objective

  • Confirm Docker Compose is available.
  • Create a working directory for the stack.
  • Create a minimal docker-compose.yml (nginx + redis).
  • Bring up the stack in detached mode.
  • Verify service status with docker compose ps.
  • Validate nginx responds on the published port.
  • Inspect recent logs for the web service.
  • Tear down the stack and remove the lab directory.

Concepts

  • Docker Compose defines a multi-container application in a single YAML file and manages lifecycle as a unit.
  • docker compose up -d creates a project network and starts service containers in the background.
  • docker compose ps shows service state and published ports for the current project directory.
  • docker compose logs supports service-scoped log inspection for quick triage.
  • docker compose down stops and removes containers and the project network created by Compose.

Walkthrough

Step 1 : Verify Docker Compose is installed.
Command
docker compose version
Docker Compose version v2.24.6
Step 2 : Create the working directory and enter it.
Command
mkdir -p /root/compose-lab && cd /root/compose-lab
(directory ready)
Step 3 : Create docker-compose.yml (nginx + redis).
Command
cat > docker-compose.yml << 'EOF'
services:
  web:
    image: nginx:latest
    ports:
      - "8081:80"
  cache:
    image: redis:latest
    ports:
      - "6379:6379"
EOF
(docker-compose.yml created)
Step 4 : Start the stack in detached mode.
Command
docker compose up -d
[+] Running 2/2
 ✔ Container compose-lab-web-1    Started
 ✔ Container compose-lab-cache-1  Started
Step 5 : Show compose service status.
Command
docker compose ps
NAME                 IMAGE          STATUS          PORTS
compose-lab-web-1     nginx:latest   Up              0.0.0.0:8081->80/tcp
compose-lab-cache-1   redis:latest   Up              0.0.0.0:6379->6379/tcp
Step 6 : Verify nginx responds on localhost:8081.
Command
curl -I http://localhost:8081
HTTP/1.1 200 OK
Server: nginx/1.25.x
Step 7 : Show the last 5 lines of logs for the web service.
Command
docker compose logs --tail 5 web
web-1  | 127.0.0.1 - - [10/Feb/2026:23:00:00 +0000] "HEAD / HTTP/1.1" 200 0 "-" "curl/7.88.1"
Step 8 : Stop and remove the stack.
Command
docker compose down
[+] Running 2/2
 ✔ Container compose-lab-cache-1  Removed
 ✔ Container compose-lab-web-1    Removed
 ✔ Network compose-lab_default    Removed
Step 9 : Remove the lab directory.
Command
rm -rf /root/compose-lab
(removed)

Common breakpoints

docker compose version fails

Confirm Docker is installed and the Compose v2 plugin is available. On some systems, Compose may be a separate package or not included by default.

curl to localhost:8081 fails

Confirm the stack is Up in docker compose ps and the port mapping is present. If the port is already in use on the host, the web service may fail to start.

docker compose logs is empty

Generate traffic with curl, then re-check logs. Nginx may not emit access logs until a request is made.

Cleanup checklist

Ensure the stack is down and the working directory is removed.

Commands
cd /root || true
docker compose -f /root/compose-lab/docker-compose.yml down 2>/dev/null || true
rm -rf /root/compose-lab
Success signal

docker compose ps shows no running services for the project and /root/compose-lab no longer exists.

Reference

  • docker compose version : Shows Docker Compose version.
  • docker compose up -d : Creates and starts services in the background.
    • -d : Detached mode (runs in background).
  • docker compose ps : Shows service status and ports for the current project.
  • docker compose logs --tail <N> <service> : Shows the last N log lines for a service.
    • --tail <N> : Limits output to the last N lines.
  • docker compose down : Stops and removes containers and the project network.
  • curl -I <url> : Fetches HTTP response headers for a quick health check.
    • -I : Requests headers only (HEAD).
  • cat > <file> << 'EOF' : Creates a file using a heredoc.
    • 'EOF' : Prevents variable expansion while writing the file.
  • rm -rf <path> : Removes a directory recursively without prompting.
    • -r : Recursive removal.
    • -f : Force removal, no prompts.