- Beginner
- Networking
How it works
Start both containers with --network shop, and the API reaches PostgreSQL at db:5432, whatever its IP. --network-alias adds extra names. Containers talk over the network on their own ports: no -p is needed between them.
What breaks
The name to use is the container name (or the Compose service name), not the image name. And a container only resolves names on the networks it is attached to: docker network connect adds a running container to another network.
In a terminal
docker network create shop
docker run -d --name db --network shop -e POSTGRES_PASSWORD=example postgres:16
docker run --rm --network shop alpine:3.20 ping -c 1 db
See it break, then fix it
Write the Dockerfile or the compose file, type the docker commands, and watch a simulated server run them through reboots, upgrades and traffic. Free to start.