# 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.

URL: https://levelrail.com/learn/deploy-operate/private-networking-between-services/
Updated: 2026-11-27

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](https://levelrail.com/learn/deploy-operate/what-is-a-reverse-proxy/)). For how Docker networks work, see [Docker networks explained](https://levelrail.com/learn/docker/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](https://levelrail.com/learn/docker/dns-inside-docker/)). Put the hostname in an environment variable, as in [environment variables and secrets](https://levelrail.com/learn/deploy-operate/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](https://levelrail.com/learn/docker/what-0-0-0-0-means-when-publishing-ports/)).

## Step 3: Add a firewall as a second layer

Even with careful Compose files, a host [firewall](https://levelrail.com/learn/glossary/#firewall)) catches mistakes. Allow SSH, HTTP and HTTPS, and deny the rest by default, as in [set up a firewall with UFW](https://levelrail.com/learn/server-basics/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](https://levelrail.com/learn/docker/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.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

| 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

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

## Related lessons

- [Docker networks explained](https://levelrail.com/learn/docker/docker-networks-explained/)) for the building blocks used here.
- [Publish container ports safely](https://levelrail.com/learn/docker/publish-container-ports-safely/)) for why published ports can bypass the firewall.
- [What is a reverse proxy](https://levelrail.com/learn/deploy-operate/what-is-a-reverse-proxy/)) for the one public entry point.
- [Set up a firewall with UFW](https://levelrail.com/learn/server-basics/set-up-a-firewall-with-ufw/)) for the host-level second layer.
- [Environment variables and secrets](https://levelrail.com/learn/deploy-operate/environment-variables-and-secrets/)) for passing hostnames and credentials safely.

## Next

Continue with [canary releases explained](https://levelrail.com/learn/deploy-operate/canary-releases-explained/)).

## 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.
