Compose Basics

Why Compose?

Real apps have several containers: an API, a database, a cache, a worker. Typing long docker run commands is error-prone. Docker Compose describes the whole app in one compose.yaml file and starts it with one command.

A First compose.yaml

Example

compose.yaml

services:
  web:
    image: nginx:1.27
    ports:
      - "8080:80"
  cache:
    image: redis:7-alpine

Example (run it)

docker compose up -d
docker compose ps

Example output

[+] Running 3/3
 ✔ Network demo_default    Created
 ✔ Container demo-cache-1  Started
 ✔ Container demo-web-1    Started
NAME           IMAGE            SERVICE   STATUS         PORTS
demo-cache-1   redis:7-alpine   cache     Up 3 seconds   6379/tcp
demo-web-1     nginx:1.27       web       Up 3 seconds   0.0.0.0:8080->80/tcp