Compose applications

Describe services with Compose

Replace a collection of long docker run commands with one versioned description of the application services.

8 minute lesson

~~~

Docker Compose describes related containers, networks, volumes, and configuration in compose.yaml.

A service is a repeatable container configuration. Compose gives services a shared default network and uses service names for DNS. It does not merge the services into one container.

services:
  api:
    build: .
    ports:
      - "8080:3000"
  database:
    image: postgres:17-alpine
    volumes:
      - database-data:/var/lib/postgresql/data

volumes:
  database-data:

Treat the file as desired configuration. docker compose up compares it with the current project and creates or replaces resources as needed. The project name also namespaces generated container, network, and volume names, which is why the same file can run as separate projects.

Always read docker compose config before a risky change. It resolves interpolation and merged files, showing the configuration Docker will actually use without starting containers. In production-like environments, pin external image versions and review the rendered output for unintended host ports, bind mounts, or missing variables. Compose records how services should run; it does not make application data migrations or secret distribution safe automatically.

Create the file and run docker compose config to see the normalized configuration before starting anything.

Lesson completed

Take this course offline

Get every free book and course as PDF and EPUB files.

Get the download library →