You need Docker with Compose (set up in lesson 3.1.1), git and curl.
Check your tools, and curl on Windows
Run docker compose version; any v2 or later works. In Windows PowerShell, type curl.exe wherever this lesson says curl.
No git? Download https://github.com/computesphere-samples/learn/archive/refs/heads/main.zip, unzip it, and open learn-main/labs/compose-stack in a terminal.
Get the sample
git clone https://github.com/computesphere-samples/learn.git cd learn/labs/compose-stack lsAlready cloned
learnin lesson 3.4.5? Skip the clone: fromlearn/labs/compose-stack/app, where lesson 3.5.3 left you, runcd ...compose.yamlis the file from the last lesson. The deploy file is for the lab after the next lesson.You should seeapp, compose.yaml, compose.deploy.yaml and README.md.
Start the stack
docker compose up --build -d--buildbuildswebfrom./appfirst, and-druns the stack in the background so you keep your terminal. The first run downloads Postgres, which takes a minute.Port 3000 is already in use?
Change
"3000:3000"underwebincompose.yamlto"3001:3000", run the command again, and use port 3001 below. Only the port on your side changes; the app still listens on 3000 inside its container.You should seeContainer compose-stack-db-1 Healthy, then Container compose-stack-web-1 Started.
See what's running
docker compose psOnly
webpublishes a port. The database is reachable fromweb, not from your machine.You should seedb Up (healthy) with 5432/tcp, and web Up with 0.0.0.0:3000->3000/tcp.
Ask the app if it's healthy
curl -i localhost:3000/healthzTo answer, the app opened a connection to
dband ran a query.You should seeHTTP/1.1 200 OK and {"status":"ok","database":"connected"}
Stop the database
docker compose stop db curl -i localhost:3000/healthz docker compose logs webThe app is still up, but it can't do its job, so its health check says so. That 503 is what a platform acts on.
Check yourself
You should seeHTTP/1.1 503 Service Unavailable and {"status":"unhealthy","database":"unreachable"}, then a log line ending database check failed: getaddrinfo ENOTFOUND db.
Bring it back
docker compose start db curl localhost:3000/healthzThe app didn't restart. It checks the database on every request, so it recovers as soon as the database is back.
You should see{"status":"ok","database":"connected"}
Tear it down
docker compose down -v --rmi localdownremoves the containers and the network.-valso deletes thedb-datavolume and every row in it; leave it off to keep the data for next time.--rmi localremoves thewebimage Compose built, and keepspostgres:16, which it only pulled.You should seeEach container Removed, then Image compose-stack-web:latest Removed, Volume compose-stack_db-data Removed and Network compose-stack_default Removed.