Sub topic 11
Letting containers talk to each other
This chapter assumes you can build and run an image, publish a port with -p, and pass environment variables with -e. By the end you will know how containers find each other by name, and why localhost inside a container does not point at your other containers.
Each container has its own network identity
Every container gets its own network namespace. That means its own network interface, its own IP address, and its own localhost. If your app connects to localhost:5432 inside a container, it looks for Postgres inside the app container, not on your machine and not in another container.
Docker connects containers through virtual networks. The default bridge network works, but it has no name resolution. Containers on it can reach each other only by IP address, and those addresses change whenever a container is recreated.
A user-defined bridge network fixes this. Docker runs an embedded DNS server on it, so each container is reachable by its name.
Create a network and attach containers
Create the network, then start the database and the app on it. The app reaches the database at the hostname db.
docker network create appnet
docker run -d --name db --network appnet \
-e POSTGRES_USER=app -e POSTGRES_PASSWORD=secret -e POSTGRES_DB=app \
postgres:16
docker run --rm -d --name myapp --network appnet -p 3000:3000 \
-e DATABASE_URL=postgres://app:secret@db:5432/app \
myapp:1.0Check that the name resolves from inside the app container:
docker exec myapp ping -c 1 db
docker network inspect appnetdocker network inspect lists the connected containers and their IP addresses.
Published ports are for the host only
Containers on the same network talk to each other directly on the container's own port. They do not use published ports.
| Who connects | Address to use | Needs `-p`? |
|---|---|---|
| Your browser on the host | `localhost:3000` | Yes |
| `myapp` to Postgres | `db:5432` | No |
| `myapp` using `-p 5433:5432` on db | still `db:5432` | No |
Notice that the database in the example has no -p flag. Only your machine needs to reach the app, so only the app is published. Leaving the database unpublished keeps it unreachable from outside the network.
Common errors
getaddrinfo ENOTFOUND db: the containers are not on the same user-defined network, or one is on the default bridge. Rundocker network inspect appnetand check that both appear.ECONNREFUSEDondb:5432: the name resolved, but Postgres is not ready yet. The database needs a few seconds to start. Retry in the app, or use a health check, which a later chapter covers.- Connecting to the wrong port: use the port the service listens on inside its container (5432 for Postgres), not a host port you mapped.
- Name conflicts:
--namemust be unique. Remove an old container withdocker rm -f dbbefore reusing the name.
Clean up when you are done:
docker stop myapp db
docker rm db
docker network rm appnetCompose creates a network like this for you automatically and names each container after its service. That is why the Compose file in a later chapter can use db as a hostname with no extra setup.
Exercise
Your app container runs with -p 8080:3000 and a Postgres container named db runs with -p 5433:5432. Both are on a user-defined network called appnet. Which host and port should the app use to connect to Postgres, and which host and port should you use from a database tool running on your own machine?
Show answer
The app uses db:5432, because containers on the same network use the container name and the container's own port. The tool on your machine uses localhost:5433, because the host reaches the container through the published port.