liveBook · Manning

Docker Compose

As explained in Rust Servers, Services, and Apps

Docker Compose is a tool for defining and operating multi-container applications from a declarative configuration.

Docker Compose runs an application stack whose components live in separate Docker containers. A single YAML file describes each service’s image or build instructions, environment variables, storage, and network settings. Compose then provides commands to build, start, and stop the stack together. section 12.5

For a Rust web service backed by Postgres, create docker-compose.yml with a db service and an api service. The database uses a Postgres image, initialization scripts, and a volume that keeps its data outside the container’s lifecycle. The API builds from its Dockerfile and receives DATABASE_URL and HOST_PORT through its environment. section 12.3.1 section 12.5

Bring up and check the two-container application incrementally:

  • Start the database alone with docker compose up db -d. Check the connection settings and database before building the API. section 12.5
  • Load DATABASE_URL into the shell, then run docker compose build api. This matters because sqlx checks the database at compile time. section 12.5
  • Start the stack with docker compose up -d. In the demonstrated setup, connect both containers to the custom Docker network and inspect it to verify their membership. section 12.5

Check the running API

When the API connects to Postgres through the Docker network, DATABASE_URL uses the database container name rather than localhost. A request to curl localhost:3000/tutors/ then returns the seeded tutor records. section 12.5

version: '3'
services:
  db:                                         #1
    container_name: tutor-postgres
    restart: always
    image: postgres:latest
    environment:
     - POSTGRES_USER=postgres
     - POSTGRES_PASSWORD=postgres
     - POSTGRES_DB=ezytutors
    volumes:
     - tutor-data:/var/lib/postgresql/data
     - ./c12-data/initdb.sql:/docker-entrypoint-initdb.d/initdb.sql
     - ./c12-data/init-tables.sql:/docker-entrypoint-initdb.d/init-tables.sql
    ports:
     - 5432:5432
    networks:
     - tutor-network
  api:                                       #2
    restart: on-failure
    container_name: tutor-webservice
    build:                                   #3
      context: ./
      dockerfile: Dockerfile-tutor-webservice
      network: host
    environment:                             #4
     - DATABASE_URL=${DATABASE_URL}
     - HOST_PORT=${HOST_PORT}
    depends_on:
     - db                                    #5
    ports:
     - ":3000:3000"                          #6
    networks:
     - tutor-network                         #7
volumes:
        tutor-data:
networks:
        tutor-network:

Inspect the Compose YAML excerpt for the database image, persisted data, API build context, and environment-variable wiring. section 12.5

Compose simplifies container lifecycle management, but starting containers is only part of production deployment. Security, secrets, monitoring, and backups still require separate attention. section 12.6 section 12.0

How Rust Servers, Services, and Apps covers it

How deep it goes
Works through YAML services and build, startup, networking, and request checks; leaves broader production operations outside scope.
Examples
Uses a two-service docker-compose.yml, Compose commands, network inspection, and a curl request returning database-backed records.
What it assumes
Assumes an existing Rust web service and Docker containers; introduces Compose installation checks and the YAML service configuration.
What you can do after
Operate/configure a Rust API and Postgres stack with Docker Compose, then verify connectivity and an HTTP response.
How it connects
Connects Dockerfiles, Postgres, Docker volumes, custom Docker networks, environment variables, and sqlx compile-time checks.

Also explained in

Compare how 15 books explain Docker Compose →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust Servers, Services, and Apps. The section links point to the parts of the book it draws on.