HANDS-ON TASK · 1 OF 1

Compose an API and database

Project: Compose an API and database

You're a DevOps engineer at esc bash. The orders team has a small API that records orders in Postgres, and they want it running as a proper two-service stack anyone can bring up with one command - with the database's data safe on a volume.

Set up the project

Run this first. It creates /root/orders-stack with the API source and a working Dockerfile:

bash
curl -fsSL https://raw.githubusercontent.com/Esc-Bash/project-init-scripts/main/docker/project-2/init.sh | bash

The API listens on port 8000 and exposes two paths: /add records an order, and /count returns how many orders exist. It reads its database connection from environment variables - the host is the database's service name, plus a user, password, and database name. The source and Dockerfile are done; your job is the Compose file.

What to build

Write /root/orders-stack/compose.yaml describing two services:

  1. api - built from the provided Dockerfile, publishing host port 8080 to the app's port 8000, with the environment variables the app needs to reach the database, and depending on the database.
  2. db - a postgres:16-alpine image, configured with a user, password, and database (matching what the api expects), storing its data on a named volume mounted at /var/lib/postgresql/data.

Set up depends_on so the api waits for the database, ideally with a health check so it waits for Postgres to be ready, not just started. Then bring the stack up with Compose.

When it works, curl http://localhost:8080/add followed by curl http://localhost:8080/count shows the count rising, and the data lives on the volume. Everything here was covered across the Compose and networking topics.

Press Submit when the stack is up and the count increments.

Start the lab on the right to run checks.

Pass the checks to continue
Spin up a fresh environment and practice live.
docker · fresh machine · ready in under a minute