Linux Labs

Containers: Docker and compose / Stage 8 of 11

docker compose: a stack in one file

Not running Starting usually ~20 s
Leave the lesson

Step 2 of 7 · on a machine · 6 min

The stack file

Here: what compose.yaml describes and what compose creates from that file.

docker compose reads compose.yaml in the current directory. Each entry under services: is a service: compose creates a container from it. A service's keys mirror the options of docker run:

yaml
services:
  web:
    image: nginx:alpine
    ports:
      - "8080:80"        # -p 8080:80
    depends_on:
      - app              # start after app
  app:
    image: busybox
    command: ["sh", "/app/run.sh"]
    environment:
      LOG_LEVEL: info    # -e LOG_LEVEL=info
    volumes:
      - ./app:/app:ro    # a directory next to the file
    restart: unless-stopped

The project name is the directory name. The lesson's stack in ~/stack has the services proxy and backend, and their containers are called stack-proxy-1 and stack-backend-1. compose creates the network stack_default for the project and connects every service to it. A service name works there like a container name in a Docker network of your own: proxy finds backend by name. The networks: key gives a service other networks — services see each other only when they share one.

Stack commands are run in the directory with the file: docker compose ps shows the state of the services, docker compose logs <service> shows one service's log, and with --tail N its last lines.

Exercise · look

Read the service logs

The stack in ~/stack is up, and the site has already had requests. Read the last lines of the backend and proxy logs.

Sign in and start The machine is available after signing in
Ubuntu 24.04
Loading…