Kenridev Playgrounds
← all labs

The API cannot talk to its database

Three services, one network, and an API returning 500. Everything is up. Nothing works.

docker
This one did not come out of an incident. It was built to exercise multi-service labs on this platform, and the breakage it uses is the one a restore leaves behind: roles and grants live outside a database dump, so a database can come back whole and still refuse the application that owns it. Building it broke the platform twice, which was not the plan but was the useful part.

Tasks

  1. 01

    Let the API authenticate against the database

  2. 02

    Make the API's endpoint answer 200

  3. 03

    Insert one order and see the API report it

Three services are running. The database is up. The API is up. They are on the same network and can resolve each other by name.

The API still answers 500.

What you have

Service What it is
shell where you are, with psql already pointed at the database
api one endpoint on port 8080: it counts the rows in orders and tells you what happened
db Postgres 16, with a database called shop

Your shell is already configured as the superuser:

psql -c 'select version()'

The API is not you. It logs in as $APP_DB_USER with $APP_DB_PASSWORD, which are in your environment too — looking up what an application was given is part of the job.

Start here

Open the API from the Ports button above, or ask it from the shell:

wget -qO- http://api:8080/

Whatever it says, it is telling you the truth. The whole lab is in that message, and the fastest way through is to read it, fix exactly what it names, and ask again.

The shape of it

There are three things wrong, and they only become visible one at a time. That is not a trick: it is what debugging a stack actually feels like. Each fix reveals the next error, and the next error is a different error. Read each one.

Where this one came from

Not from an incident, and it would be easy to pretend otherwise.

It was built to exercise multi-service labs on this platform, and the breakage it uses is the one a restore leaves behind. Roles and grants are cluster-level objects: they live outside a database dump. So a database can come back whole — every table, every row — and still refuse the application that owns it, which is a confusing way to find out that "the database is back" and "the service works" are two different claims.

Building it broke the platform twice. A script copied into /tmp would not run, because the workstation mounts it noexec; and the stack was being handed over before Postgres had finished initialising, so the first error you met was the wrong one. Both are fixed. Neither was planned, and that is usually how this goes.

Notes

The stack is a real compose.yaml and you cannot edit it from here — this lab is about the state inside the database, not about the file. Everything you need to do, you do with psql.

Nothing here survives the session. When the lab is destroyed the database goes with it, so experiment freely.

30 mindifficulty 3 of 5dockercomposepostgresdebugging

Loading saved progress…