Connecting to Postgres in docker-compose

Dumb question and one I used to know the answer to… but how does one connect to a postgres instance running inside docker-compose? I thought I would work through Broadway’s example repo GitHub - elixir-broadway/broadway_bike_sharing_rabbitmq_example: An example of a Broadway pipeline for a bike sharing app with RabbitMQ and PostgreSQL · GitHub – it uses docker-compose to spin up both a version of Postgres (one specifically with postgis installed) and an instance of RabbitMQ.

docker-compose up seems to have worked… no errors, and Docker Desktop shows 3 containers: rabbitmq-1, db-1, and app-1. RabbitMQ and Postgres are running, but the app has crashed because it can’t connect to the database.

Looking more closely, it seems like the port directive in docker-compose.yml is being ignored. I’ve deleted the images and rebuilt this several times in the course of testing it, and each time the ports used come up with something more random (?) like 53665. If I set my database client to use that port, I can connect.

Can someone clarify why this port is changing? And I seem to remember that the list of services defined inside of the docker-compose.yml corresponded to hostnames? Is that accurate? Like, instead of localhost, you might reference a hostname of db – but that doesn’t seem to work (even if I use the numeric extension).