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














