Skip to content

Docker Compose Basics

Docker Compose runs multi-container applications from a YAML file. In course labs, Compose is useful for starting a web app, database, and supporting services with one command.

Goal

Confirm the Docker Compose plugin is available, then run a small web server from a compose.yaml file.

1. Prerequisite

Complete Docker Setup on Ubuntu Server first.

Check Docker:

docker --version
docker compose version

If docker compose version fails, install the Compose plugin:

sudo apt update
sudo apt install -y docker-compose-plugin

2. Create a Compose Lab Folder

mkdir -p ~/course-work/compose-test
cd ~/course-work/compose-test

3. Create compose.yaml

nano compose.yaml

Paste:

services:
  web:
    image: nginx:alpine
    ports:
      - "8080:80"

4. Start the Service

docker compose up -d

Check status:

docker compose ps

Test locally on the server:

curl http://localhost:8080

Expected: HTML from the Nginx welcome page.

5. Stop and Clean Up

docker compose down

Confirm no Compose containers remain:

docker ps

6. Common Compose Commands

Task Command
Start services in foreground docker compose up
Start services in background docker compose up -d
Stop and remove services docker compose down
View logs docker compose logs
Follow logs docker compose logs -f
Show service status docker compose ps
Rebuild images docker compose build

7. Classroom Checkpoint

Record:

docker compose version
docker compose ps
curl -I http://localhost:8080

Troubleshooting

Problem Fix
docker: permission denied Use sudo or complete the Docker group step from the Docker setup page.
Port 8080 already in use Change 8080:80 to another host port, such as 8081:80.
YAML parsing error Check indentation. YAML uses spaces, not tabs.

Next Step

You are ready to run containerized course labs.