Chapter 4 · Lesson 16 of 18
Deploy and operate

Private networking between services: keep internals off the internet

Private networking means only the reverse proxy is reachable from the internet, while databases and internal services talk to each other on a network the outside cannot enter.

By the end you will

Lay out a stack where only the proxy is public, internal services have no published ports, and cross-server traffic stays private.

Time
14 min
Level
Intermediate
Check
4 questions
You need
  • A Docker Compose app with a database
  • A server with a firewall you can edit

A private network layout has one public door and everything else behind it. The reverse proxy listens on ports 80 and 443 and forwards requests to the app. The app talks to the database, cache and internal APIs over a private network that only your own containers can join. Internal services publish no ports at all, so the internet has nothing to connect to, and no firewall mistake can expose them.

How it works#

On one host, Docker creates a network per Compose project. Containers attached to it get private addresses and resolve each other by service name. A port is only visible outside the host if you publish it with ports:. Without that line the service is reachable inside the network and nowhere else.

That gives you a simple rule: publish what clients must reach, and nothing else. Clients are browsers and API users, so that is the proxy. For the proxy itself, see what is a reverse proxy. For how Docker networks work, see Docker networks explained.

Step 1: Publish only the proxy#

A Compose file where only the proxy has a public port:

yaml
services:
  proxy:
    image: caddy:2
    ports:
      - "80:80"
      - "443:443"
    networks:
      - public
      - internal
  app:
    image: myapp:1.4.0
    environment:
      DATABASE_HOST: db
    networks:
      - internal
    depends_on:
      - db
  db:
    image: postgres:16
    networks:
      - internal
    volumes:
      - dbdata:/var/lib/postgresql/data

networks:
  public:
  internal:
    internal: true

volumes:
  dbdata:

The app and db services have no ports. The internal network is marked internal: true, which blocks its containers from reaching the outside world. That is stricter than many apps need, since an app often has to call external APIs, so use it only for services that must not make outbound connections, or give the app a second network that does have outbound access. The proxy sits on both networks so it can reach the app.

Step 2: Connect by service name#

The app reaches the database at db:5432, using the container port, with no published mapping involved. This works because of the embedded DNS described in DNS inside Docker. Put the hostname in an environment variable, as in environment variables and secrets, so the same image works in other setups.

If you do need to reach an internal service from the server itself, for example to run a one-off database client, bind it to loopback rather than all interfaces:

yaml
    ports:
      - "127.0.0.1:5432:5432"

Only the host can then connect, and remote access goes through an SSH tunnel. The address rules are explained in what 0.0.0.0 means when publishing Docker ports.

Step 3: Add a firewall as a second layer#

Even with careful Compose files, a host firewall catches mistakes. Allow SSH, HTTP and HTTPS, and deny the rest by default, as in set up a firewall with UFW. Remember that Docker’s published ports can bypass UFW, which is why the “no ports on internal services” rule matters more than the firewall rule. The details are in publish container ports safely.

Docker’s bridge networks live on one host. When the database or an internal API runs on another server, the traffic between servers should not cross the public internet in clear text. Two common patterns, described generically:

  • A private network from your hosting provider, sometimes called a VPC or private network. Servers in the same region get private addresses that are not routed from the internet. Bind the internal service to that private address and allow only the app server’s private address in the firewall.
  • A WireGuard tunnel between servers. Each server gets a tunnel address, such as 10.8.0.1 and 10.8.0.2, and traffic between them is encrypted. Bind the service to the tunnel address and permit the tunnel interface in the firewall.

Check your provider’s documentation for how private networking is offered and whether it is free. In both patterns, the service still needs authentication. A private network reduces exposure, but it is not a replacement for passwords and TLS where data is sensitive.

Check it worked#

From your own computer, scan for ports that should not be open:

bash
nc -zv 203.0.113.10 5432
nc -zv 203.0.113.10 443

Port 5432 should fail to connect or time out, and 443 should succeed. Only scan servers you own. On the server, list the listeners:

bash
ss -ltn

Look for 0.0.0.0: and [::]: lines. Only 22, 80 and 443 should appear as public listeners. Anything else is either bound to 127.0.0.1 or a private address, or should be removed. Then confirm that the app still reaches the database:

bash
docker compose exec app getent hosts db
docker compose logs --tail 20 app

Edge cases and gotchas#

  • Convenience ports: A ports line added to debug a database often stays forever. Search your Compose files for any internal service that publishes.
  • Admin tools: Web UIs for databases or queues should not be public. Put them behind the proxy with authentication, or use an SSH tunnel.
  • internal: true: It blocks outbound traffic from that network too, so a service on it cannot pull data from the internet or send email.
  • Name collisions: Two Compose projects have separate networks. To share a service, create an external network and attach both.
  • Private is not secure by itself: Another compromised service on the same network can reach the database. Use separate credentials per service and consider separate networks per tier.
  • IPv6: A service published on [::] can be open over IPv6 even if you only checked IPv4. Review ss -ltn output for both.

Troubleshooting#

SymptomLikely causeFix
App cannot reach db by nameDifferent networksAttach both services to the same network
Database reachable from the internetports entry on the databaseRemove it, recreate the container, rescan
App fails to call an external APIService only on an internal: true networkAdd a second network with outbound access
Cross-server connection times outFirewall blocks the private addressAllow the other server’s private address on that port
Tunnel works, but service refusesService bound to 127.0.0.1 onlyBind it to the tunnel or private address

Quick reference#

bash
docker network ls
docker network inspect <project>_internal
docker compose exec app getent hosts db
ss -ltn
sudo ufw status verbose

Next#

Continue with canary releases explained.

Check what you learned4 questions
1In a safe layout, which service publishes ports to the internet?
2How does the web container reach a database service named db on a Compose network?
3Why is a database with ports: - "5432:5432" risky?
4Which option can link containers on two different servers privately?

Frequently asked questions

How do I keep my database private with Docker Compose?

Do not add a ports entry to the database service, and put it on a network shared only with the services that need it. Other containers connect using the service name.

Do I need to publish ports between containers?

No. Containers on the same Docker network reach each other on container ports directly. Publishing is only for traffic coming from outside the host.

How do I connect services running on two servers privately?

Use a private network from your hosting provider if it offers one, or a WireGuard tunnel between the servers. Then bind the internal service to the private address.

Is a firewall still needed if my database has no published port?

Yes. A firewall is a second layer that protects the host's own services such as SSH and catches mistakes if someone later adds a published port.

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios