Up, Down, Rebuild

Bringing a Project Up, Down and Rebuilding

docker compose up -d builds missing images, creates missing resources and starts the containers in dependency order; run again, it changes only what differs from the file. The first start of BookNest shows why "in order" is not enough:

Status, logs and a rebuild after a code changeShell
docker compose ps --format '{{.Service}}  {{.Status}}  {{.Ports}}' | sed 's/, \[::\].*//'
sleep 5; docker compose logs api | grep -E '^api-1  \| (Failed|BookNest)' | cut -c1-82
docker inspect l3-booknest-api-1 --format 'restarts: {{.RestartCount}}'
curl -s localhost:33000/api/books/3 | cut -c1-60
sed -i "s/{ status: 'ok' }/{ status: 'ok', version: '1.3' }/" app.js
docker compose up -d --build 2>&1 | grep -E 'Built|Recreated|Running'
sleep 2; curl -s localhost:33000/health; echo; git checkout -q app.js
Output
api  Up Less than a second (health: starting)  0.0.0.0:33000->3000/tcp
db  Up 2 seconds  127.0.0.1:33432->5432/tcp
api-1  | Failed to start BookNest API Error: connect ECONNREFUSED 172.19.0.2:5432
api-1  | Failed to start BookNest API Error: connect ECONNREFUSED 172.19.0.2:5432
api-1  | BookNest API listening on http://localhost:3000
restarts: 2
{"id":3,"title":"Salt and Saffron","author":"Priya Nair","ge
 Image l3-booknest-api Built
 Container l3-booknest-db-1 Running
 Container l3-booknest-api-1 Recreated
{"status":"ok","version":"1.3"}

depends_on started db first, but only waited for its container to start, not for PostgreSQL 1,289 to accept connections, so the API crashed with ECONNREFUSED while PostgreSQL initialized (some runs also logged the database system is starting up), and restart: on-failure retried until it connected. Healthchecks replaces this with a healthcheck condition. After the edit, --build rebuilt only the API image and recreated only its container; db kept running. The other everyday commands: docker compose logs -f api follows one service, docker compose restart api restarts without recreating, docker compose up -d --wait returns only when every service is running or healthy, docker compose down removes the containers and network but keeps the volume, and docker compose down -v deletes the data too.