Skip to content
VDS · Docker and containers

Docker networks and port forwarding

How containers see each other, how publishing a port differs from a simple EXPOSE, and why Docker bypasses ufw rules.

Updated August 23, 2026

Half of container problems are network-related: a service can't reach the database, a port is taken, the control panel is suddenly exposed to the entire internet. Let's break down how this works.

Three types of networks

  • bridge — the default. Containers get internal addresses and communicate with each other; they go out through the host's NAT.
  • host — the container uses the host's network directly; port publishing is neither needed nor works. Fast, but no isolation: the container's port 80 is the host's port 80.
  • none — no network at all. For tasks that don't need one.

A custom network created by you or by Compose differs from the default bridge in one important way: name resolution works in it. The app container can reach the db container by the name db.

docker network create backend
docker network ls
docker network inspect backend

In Compose, such a network is created automatically for each project.

Port publishing

ports:
  - "127.0.0.1:8080:8080"   # accessible only from the machine itself
  - "80:80"                 # accessible to the whole internet

On the left is the port on the host, on the right is the port inside the container. The EXPOSE directive in an image doesn't publish anything: it's just a note about which port the application listens on.

The rule is simple: publish only what must be accessible from the outside. The database, control panel, and internal API go on 127.0.0.1, and a web server with a certificate exposes them to the outside.

Docker writes iptables rules itself and does so before the chains managed by ufw. A port published as -p 8080:8080 will be accessible from the internet even if ufw shows “deny”. Don't rely on the firewall — bind the port to 127.0.0.1.

Communication between containers

services:
  app:
    networks: [frontend, backend]
  db:
    networks: [backend]
  proxy:
    networks: [frontend]
    ports:
      - "80:80"
      - "443:443"

networks:
  frontend:
  backend:

Here the database is accessible only to the application, and only the web server faces the outside. This is the right setup for any stack.

Inside a container, refer to a neighbor by its service name: postgres://db:5432/app. The address localhost inside a container means the container itself, not the host — the second most common mistake after latest.

To reach a service running on the host itself from a container, use the name host.docker.internal by adding:

extra_hosts:
  - "host.docker.internal:host-gateway"

Diagnostics

docker compose exec app ping -c2 db
docker compose exec app getent hosts db
sudo ss -tulpn | grep LISTEN
docker port <container>

ss will show which interface a port is actually listening on: 0.0.0.0:8080 — open to the outside, 127.0.0.1:8080 — not.

Common mistakes

  • “Port already in use”. It's held by another container or a host service. sudo ss -tulpn | grep :80 will find the culprit.
  • Containers in different Compose projects can't see each other. Each project has its own network. Connect them explicitly via networks: { shared: { external: true } }.
  • The application listens on 127.0.0.1 inside the container. Then publishing won't help: you can't reach it from outside the container. Configure the application to listen on 0.0.0.0.
  • Bandwidth. Up to 500 Mbps shared per host. File distribution shares it with everything else.