Docker Networking — DevOps

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…

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:

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

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

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

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.

What you will learn