The Hidden Docker Compose DNS Trap Behind Shared Traefik Networks
Almost every self-hosting tutorial for Traefik and Docker Compose shows the same pattern: create one external proxy network, then add it to every app's docker-compose.yml: services: kong: image: kong:2.8 net
Almost every self-hosting tutorial for Traefik and Docker Compose shows the same pattern: create one external proxy network, then add it to every app's docker-compose.yml:
services:
kong:
image: kong:2.8
networks:
- default
- proxy
It works cleanly for your first app. It even looks fine for your second appβuntil two separate apps on the same VPS happen to share a Compose service name like kong, web, api, app, or db.
What most people miss is that Docker Compose registers the short service name as a DNS alias on every network declared in that Compose fileβnot just the app's private default network, but the shared proxy network too.
If App A and App B both have a service named kong attached to proxy through docker-compose.yml, Docker's embedded DNS maps kong on proxy to both containers. When App A's internal dashboard resolves http://kong:8000, it can land on App B's container and send App A's service_role key straight to the wrong app's gateway.
We hit this exact class of cross-app DNS collision while building Nitrobaseβand solving it meant changing how shared networks are attached across every container lifecycle command.
Nitrobase has only recently launched and is still in Beta. This pattern is not full Kubernetes network policy or L7 service mesh isolation. Its goal is narrower and practical for a single VPS: make sure containers sharing a reverse-proxy or database-link network can never accidentally resolve another app's service name.
Why docker network connect behaves differently from Compose
Traefik, Nitrobase's health probe, and database links never need bare service names like kong or web on a shared networkβthey address containers by their unique container name (for example, myapp-kong).
Unlike docker compose, running plain:
docker network connect <shared-network> <container-name>
without --alias registers only the container's own unique name and short container ID on that network. No bare service name (kong, db, web) is ever registered on the shared bridge.
So Nitrobase's rule is strict: generated docker-compose.yml files never declare the shared proxy network or database link networks. Instead, Nitrobase writes a small sidecar file in the app folder (.nitrobase-networks, one <container>\t<network> pair per line, blocked from being bind-mounted into any container).
Avoiding the startup race with up --no-start -> attach -> up
The obvious naive fix is:
- Run
docker compose up -d - Run
docker network connectafterward
That creates an immediate startup race. A container that needs its database link network at bootβor needs to be reachable by Traefik from second zeroβstarts before docker network connect has run.
Instead, every compose up in Nitrobase (composeLifecycleCommand) runs in three steps:
1. docker compose up --no-start [--force-recreate ...]
| Creates/recreates containers without booting them yet
| (strips start-only flags: -d, --detach, --wait)
v
2. attachNetworksScript (.nitrobase-networks)
| Verifies container name starts with "<folder>-"
| AND label nitrobase.app == <appId> (blocks name squatting)
| Skips already-attached pairs -> docker network connect <net> <container>
v
3. docker compose up -d [without --force-recreate]
Boots every container ALREADY attached to its shared networks
Notice two subtle safeguards in that flow:
-
--force-recreateis stripped before Step 3. If you passed--force-recreateto both phases, Step 3 would immediately recreate the containers a second timeβwiping out the network attachments you just applied in Step 2! -
Ownership verification before attaching. Before running
docker network connect, the script checks that the container name starts with the app's<folder>-prefix and that itsnitrobase.appDocker label matches the expected app ID. Another container squatting a predicted name causes the attach step to fail instead of silently joining the wrong network.
For start and restart, Nitrobase attaches before starting (stopped containers retain their network attachments). For create, it attaches right after.
Detecting and self-healing foreign Compose aliases
What if an app was deployed before this rule existed, or someone manually ran docker compose up with a shared network declared in the YAML?
Nitrobase's background monitoring inspects only containers owned by the current panel instance (nitrobase.instance=<id> and nitrobase.app) and reads each container's aliases on the proxy network. Any alias other than the container's own name and short hex ID is classified as a foreign alias (foreignAliases).
planNetworkRepairs then handles two self-healing cases:
| Condition detected | Repair action | What it runs |
|---|---|---|
Container is on the proxy network with foreign Compose aliases (kong, <project>-<service>-1, etc.) |
realias |
docker network disconnect followed immediately by docker network connect (sub-second reconnect that strips all foreign aliases) |
Routed container (nitrobase.routed=true) is missing from the proxy network |
attach |
docker network connect |
To prevent an infinite disconnect/reconnect loop if something outside Nitrobase keeps re-attaching a container with Compose aliases, guardNetworkRepairs adds a flap guard: if the same container requires the same repair for more than maxAttempts consecutive monitoring ticks, Nitrobase stops reconnecting it every tick and raises a network_flap alert instead.
A bonus Docker inspect gotcha we hit along the way
While building the network inspector, we hit a surprisingly subtle Docker CLI quirk:
{{.ID}} <-- Go struct field name (stays on typed inspect result)
{{.Id}} <-- JSON key name (fails typed lookup -> Docker falls back to raw JSON!)
When a docker inspect --format template uses .Id instead of .ID, Docker fails to evaluate the Go struct template and silently falls back to evaluating the template against the raw JSON map. On containers without a healthcheck, the raw JSON omits State.Health completelyβcausing {{if .State.Health}} to error and silently drop that container from the entire inspect output. Sticking strictly to Go struct field names (.ID, .Name, .NetworkSettings.Networks) keeps evaluation on the typed struct path and avoids missing rows.
What removing Compose aliases still does not isolate
Removing shared service-name aliases prevents accidental cross-app DNS collisions and hostname hijacking across apps on the same proxy network. It does not block raw IP-to-IP TCP traffic between two containers that are both attached to the same bridge networkβif App A is compromised and actively scans the bridge subnet, it can still reach another container's port on that bridge unless you enforce L3 firewall/network isolation or separate hosts.
My checklist for multi-app Traefik + Docker Compose hosts
- [ ] Never declare a shared
proxynetwork inside per-appdocker-compose.ymlfiles when multiple apps can share service names (web,api,app,kong,db) - [ ] Attach only the routed containers via
docker network connect(no--alias) so only unique container names resolve on the shared network - [ ] Use
up --no-start->attach->up(with--force-recreateonly in phase 1) so containers boot with their networks already connected - [ ] Inspect existing containers on your shared proxy network (
docker inspect --format '{{.Name}} -> {{with index .NetworkSettings.Networks "proxy"}}{{join .Aliases ","}}{{end}}') to see which short service names are colliding right now - [ ] Add a flap guard if you run automated network repairs so external drift raises an alert instead of looping forever
Shared Docker networks feel harmless until two stacks share the word api or kong. Attaching containers outside Compose takes a few extra lines of lifecycle logic and eliminates an entire class of silent cross-app leaks.
Nitrobase source code: https://github.com/AydenGroup/nitrobase
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.