Dmitry Troshenkov

A website container that could reach the CRM database

· 5 min read

I run two things on one small server: a marketing website for a client, and a CRM I’m building for contractors. Each is its own Docker Compose project. The website has one job that touches the CRM. When someone fills in the quote form, it sends the lead to the CRM API.

To make that call work, the website container was added to the CRM’s Docker network. It worked. It also meant that from inside the website container I could open a connection to:

ServicePort
CRM database (Postgres)5432
Redis, no password6379
CRM frontend80
CRM marketing site80
Tunnel (cloudflared) metrics20241

The website is the most exposed part of the setup. It is public, it takes form input from anyone, and it loads third-party scripts for ads and analytics. If someone ever found a way to run code in it, the CRM database would be one hop away.

Why it happens

Joining another project’s network is the usual way to let two Compose stacks talk. A Docker network has no rules inside it. Every container on it can reach every port of every other container on it. So a website that needs one API call can also reach everything else the CRM runs.

What I did

I added a second network on the CRM side with only one member, the backend:

# CRM docker-compose.yml
services:
  gcframe-backend:
    networks:
      - gcframe-net       # the CRM network, with the database, Redis and frontend
      - gcframe-ingest    # the only network outside stacks may join

networks:
  gcframe-ingest:
    name: gcframe-ingest  # fixed name, so it doesn't depend on the compose project name

The website leaves the CRM’s main network and joins only that one, next to its own:

# website docker-compose.yml
services:
  renovation-contact:
    networks:
      - renovation-net    # its own network, shared with its tunnel
      - gcframe-ingest    # the CRM backend and nothing else

networks:
  renovation-net:
  gcframe-ingest:
    external: true

The ingest URL in the website config (http://gcframe-backend:4000) did not change, because the backend has the same name on both networks.

The order of the rollout matters

The website now expects a network that the CRM creates. If the website is deployed first, its container can’t start because the network doesn’t exist yet. So the order was:

  1. Deploy the CRM change. It creates the new network and recreates the backend container (a short restart).
  2. Then deploy the website change.

The website deploy script rolls back automatically when the new container fails its health check, so the wrong order would have cost a failed deploy, not downtime. Still better to do it in the right order.

Proof

I ran this from inside the website container. It looks up each name, then tries a TCP connection by name and by the old IP address on the CRM network:

// docker exec -i renovation-contact node - < check.js
const net = require('net'), dns = require('dns').promises;
const targets = [
  ['gcframe-backend', '172.20.0.2', 4000],
  ['gcframe-postgres', '172.19.0.2', 5432],
  ['gcframe-redis', '172.19.0.3', 6379],
  ['gcframe-frontend', '172.19.0.6', 80],
  ['gcframe-www', '172.19.0.7', 80],
  ['gcframe-cloudflared', '172.19.0.4', 20241],
];
const tcp = (host, port) => new Promise(r => {
  const s = net.connect({ host, port, timeout: 3000 });
  s.on('connect', () => { s.destroy(); r('open'); });
  s.on('timeout', () => { s.destroy(); r('timeout'); });
  s.on('error', e => r(e.code));
});
(async () => {
  for (const [name, ip, port] of targets) {
    let addr;
    try { addr = (await dns.lookup(name)).address; } catch (e) { addr = e.code; }
    const byName = addr.includes('.') ? await tcp(name, port) : '-';
    console.log(`${name}:${port}`, `dns=${addr}`, `by-name=${byName}`, `by-ip(${ip})=${await tcp(ip, port)}`);
  }
})();

Output:

gcframe-backend:4000 dns=172.20.0.2 by-name=open by-ip(172.20.0.2)=open
gcframe-postgres:5432 dns=ENOTFOUND by-name=- by-ip(172.19.0.2)=timeout
gcframe-redis:6379 dns=ENOTFOUND by-name=- by-ip(172.19.0.3)=timeout
gcframe-frontend:80 dns=ENOTFOUND by-name=- by-ip(172.19.0.6)=timeout
gcframe-www:80 dns=ENOTFOUND by-name=- by-ip(172.19.0.7)=timeout
gcframe-cloudflared:20241 dns=ENOTFOUND by-name=- by-ip(172.19.0.4)=timeout

Only the backend answers. The other names don’t resolve, and their addresses time out. The lead health check still reports the CRM as OK.

Redis got a password too

Only the CRM’s own containers can reach Redis now, but that was no reason to leave it open. I ran into two small traps.

What is still open

The website can still reach the whole backend API on port 4000, not only the lead route. That part is protected by API keys.

Check yours

If you run more than one Compose project on a server:

If you want me to look at how your containers are connected, message me on LinkedIn.