You've just moved notes-api into a container. Its DATABASE_URL didn't change at all, and it still points at localhost:5432 like when the app ran directly on web-1. As soon as it starts, the app fails with ECONNREFUSED (connection refused), even though Postgres is clearly running in the container next door.
The reason: Docker gives your containers a virtual network of their own. Every container gets its own IP, a name other containers can call, and a localhost of its own. From outside, a container can only be reached through a port you deliberately open. Picture an office building. Every room has a phone extension. In the new building there's a receptionist: say the room's name, and you're connected right away. Calling "your own number" from a room only rings that room. From the street, people can only call rooms that have a direct outside line. In Docker, the building is a network (a virtual network), the rooms are containers, and the extension is a container's IP. The receptionist in the new building is DNS (a service that turns names into IPs) on a network you create yourself. "Your own number" is localhost, and the direct outside line is a port you open with -p.
Bridge networks: every container gets its own IP
A network driver (a kind of network) decides how containers are connected. The one used most is bridge, and bridge is also the default. On a bridge, containers on one host are joined through a virtual switch, a pretend network connector inside the host. Other drivers:
host: the container uses the host's network directly, with no port isolation (the app's port is the host's port).none: no network at all.overlay: joins containers across many hosts, for Docker Swarm (Docker's tool for running containers on many servers).
There are two kinds of bridge. The subnet in this table is the range of IPs that belongs to the network:
| The default bridge | A bridge you create | |
|---|---|---|
| How you get it | a container without --network |
docker network create notes-net |
| Example subnet | 172.17.0.0/16 |
172.18.0.0/16 |
| Call a container by name | no | yes |
Calling a container by name
On a bridge you created, Docker runs its own DNS. So a container's name (or a service's name in Compose) automatically becomes a host name (hostname), the name used to reach that container. In this example, -e sets an environment variable, and -v attaches a volume:
$ docker network create notes-net
$ docker run -d --name db --network notes-net \
-e POSTGRES_USER=notes -e POSTGRES_PASSWORD=$DB_PASSWORD \
-v db-data:/var/lib/postgresql/data postgres:16
$ docker run -d --name notes-api --network notes-net \
-e DATABASE_URL=postgres://notes:$DB_PASSWORD@db:5432/notes \
-p 127.0.0.1:8080:3000 ghcr.io/budi/notes-api:a41c9e2
$ docker logs notes-api
Server running at http://localhost:3000
--network notes-netputs both containers on the same network. From insidenotes-api, the namedbresolves to172.18.0.2.docker network inspect notes-netshows what's in that network: the subnet172.18.0.0/16, and every container with its IP (dbat172.18.0.2/16,notes-apiat172.18.0.3/16)./var/lib/postgresql/datais the data path forpostgres:16. Frompostgres:18, the volume is mounted at/var/lib/postgresql(the Docker Volumes & Data lesson).- On the default bridge, names can't be used. The app fails with
getaddrinfo ENOTFOUND db(the namedbwas not found).
Docker Compose creates a bridge of its own for each project automatically (notes-api_default). So in Compose, the service name is used as the hostname: db:5432, redis:6379. That's why the Compose file in the previous lesson could simply write @db:5432.
localhost inside a container
localhost (127.0.0.1) inside a container means that container itself, not web-1 and not another container. There's no Postgres in the app container, so the connection is refused:
Database connection failed: connect ECONNREFUSED 127.0.0.1:5432
That error appears while DATABASE_URL still points at localhost. Replace localhost in that URL with the container's or service's name: postgres://notes:...@db:5432/notes. Sometimes a container does need to call a program running directly on the host. For that, Docker Desktop provides the name host.docker.internal. On Linux, that name only works when you add --add-host=host.docker.internal:host-gateway to docker run.
Ports: published or only noted
A container on a bridge can't be reached from outside the host until its port is published with -p:
| Option | Can be opened from | Shown in docker ps |
|---|---|---|
(no -p, only EXPOSE 3000) |
other containers on the same network | 3000/tcp |
-p 8080:3000 |
web-1 and the whole internet |
0.0.0.0:8080->3000/tcp |
-p 127.0.0.1:8080:3000 |
web-1 only |
127.0.0.1:8080->3000/tcp |
- The format is
HOST:CONTAINER.-p 8080:3000means port 8080 on the host is forwarded to port 3000 in the container. Onweb-1,curl http://localhost:8080/healthis answered by the app. - With no IP in front, Docker opens the port on
0.0.0.0, meaning every address of the host, the public IP203.0.113.10(the address ofweb-1on the internet) included. With127.0.0.1:in front, only the host itself can open it. Usually Nginx onweb-1forwards requests to that port (the Reverse Proxy lesson). - Between containers on the same network, ports don't need publishing at all.
notes-apiconnects todb:5432even thoughdbhas no-p.
In the exercise, you take the text of that -p option apart. "8080:3000" becomes { host: "0.0.0.0", hostPort: 8080, containerPort: 3000 }, and "127.0.0.1:3000:3000" becomes { host: "127.0.0.1", hostPort: 3000, containerPort: 3000 }. Cut the text at :. Two parts means host (the IP) gets the default 0.0.0.0. Three parts means the first part is the IP. The ports must be numbers, so convert them with Number(...).
⚠️ Wrong addresses, ports left open
localhostfor another container. Inside a container,localhostis the container itself. Use the container's or service's name.- Relying on names on the default bridge. Names can only be called on a bridge you created (or a Compose network). On the default bridge the result is
ENOTFOUND. -p 5432:5432for a database on a server. Postgres becomes reachable from the internet. Docker opens ports with its own iptables rules (network rules in the Linux kernel). So theufwfirewall onweb-1(a program that blocks connections from outside) doesn't stop it. A database needs no-p. If you really need to reach the database from the host, use127.0.0.1:5432:5432. More hardening is in the Docker Security Basics lesson.- Ports swapped. The left is the host port, the right is the container port.
-p 3000:8080forwards to port 8080 in the container, while the app listens on port 3000.
TL;DR: Docker's default network driver (kind of network) is bridge (a virtual network for the containers on one host, the computer that runs Docker). On a bridge you create (docker network create) and on the network Compose creates, Docker has its own DNS (which turns names into IPs). So containers call each other by name (db:5432). On the default bridge (used by containers without --network), names can't be used. localhost inside a container means the container itself, not the host and not another container. EXPOSE only notes a port. What forwards a host port to the container is -p HOST:CONTAINER (-p 8080:3000). That port is opened on 0.0.0.0 (every address, the internet included), unless you write an IP in front (-p 127.0.0.1:8080:3000). Containers on the same network need no -p, and a database's port on a server must not be opened on 0.0.0.0.
⚡ Bridge: every container gets its own IP
Write:
$ docker network create notes-net
Meaning: This command creates a network (a virtual network for containers) called notes-net. It is a bridge, Docker's default kind of network: containers on one host (the computer that runs Docker) are joined through a virtual switch (a pretend network connector). Every container gets its own IP. On a bridge you create yourself, containers can call each other by name. On the default bridge (used by containers without --network), names can't be used.
⚡ Call a container by its name
Write:
$ docker run -d --name notes-api --network notes-net \
-e DATABASE_URL=postgres://notes:$DB_PASSWORD@db:5432/notes \
-p 127.0.0.1:8080:3000 ghcr.io/budi/notes-api:a41c9e2
Meaning: A Postgres container called db is already running on notes-net. The notes-api container joins the same network, so the two can call each other by name. From inside notes-api, the name db becomes the IP 172.18.0.2. So DATABASE_URL (set with -e) can simply write @db:5432. In Compose, a service's name becomes its host name (the name used to reach the container) automatically.
⚡ localhost inside a container
Write:
Database connection failed: connect ECONNREFUSED 127.0.0.1:5432
Guess first: the app's DATABASE_URL points at localhost:5432, and Postgres is clearly running in the container next door. Why is the connection refused?
localhost inside a container means that container itself, not web-1 and not another container. There's no Postgres in the app container.
Meaning: Replace localhost in DATABASE_URL with the container's or service's name: @db:5432.
⚡ -p HOST:CONTAINER
Write:
-p 8080:3000 web-1 and the whole internet
-p 127.0.0.1:8080:3000 web-1 only
Meaning: The left is the host port, the right the container port. With no IP in front, Docker opens the port on 0.0.0.0, every address of the host, the public IP (the server's address on the internet) included. So on a server, never open a database's port with -p without 127.0.0.1 in front.