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.
- Updated Nov 27, 2026
- Markdown
Lay out a stack where only the proxy is public, internal services have no published ports, and cross-server traffic stays private.
- 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:
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:
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.
Step 4: Cross-server private links#
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.1and10.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:
nc -zv 203.0.113.10 5432
nc -zv 203.0.113.10 443Port 5432 should fail to connect or time out, and 443 should succeed. Only scan servers you own. On the server, list the listeners:
ss -ltnLook 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:
docker compose exec app getent hosts db
docker compose logs --tail 20 appEdge cases and gotchas#
- Convenience ports: A
portsline 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. Reviewss -ltnoutput for both.
Troubleshooting#
| Symptom | Likely cause | Fix |
|---|---|---|
| App cannot reach db by name | Different networks | Attach both services to the same network |
| Database reachable from the internet | ports entry on the database | Remove it, recreate the container, rescan |
| App fails to call an external API | Service only on an internal: true network | Add a second network with outbound access |
| Cross-server connection times out | Firewall blocks the private address | Allow the other server’s private address on that port |
| Tunnel works, but service refuses | Service bound to 127.0.0.1 only | Bind it to the tunnel or private address |
Quick reference#
docker network ls
docker network inspect <project>_internal
docker compose exec app getent hosts db
ss -ltn
sudo ufw status verboseRelated lessons#
- Docker networks explained for the building blocks used here.
- Publish container ports safely for why published ports can bypass the firewall.
- What is a reverse proxy for the one public entry point.
- Set up a firewall with UFW for the host-level second layer.
- Environment variables and secrets for passing hostnames and credentials safely.
Next#
Continue with canary releases explained.
Check what you learned
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.
Keep going: guides and tutorials
- Zero Downtime Deploy for Docker Apps, With Rollback How a Docker deploy waits for readiness before cutover, holds the previous release for rollback, rolls back on its own, and how to rehearse it.
- Roll Back a Failed Docker Deploy Without Downtime Ship a broken release on purpose, watch your users stay on the old version, then roll back with one command. Real commands, real output, self-hosted.
- Our Health Check Failed and the Deploy Went Live Anyway A postmortem from our own dogfood run: a failed readiness probe still received traffic. What broke, why two correct parts added up to a bug, and the fix.