Description
On Linux Docker Engine, network_mode: "host" shares the host’s network namespace, so the service listens directly on its addresses and ports. Check host-network support on other runtimes.
Potential impact
A listening port can conflict with a host process or expose the service on unintended interfaces. The listening address and firewall determine reachability. Authentication controls who can use the service.
Remediation
Remove network_mode: "host" to use the Compose project’s default network or a dedicated network. Services that communicate only within Compose need no published ports. For external access, publish only the required host addresses and ports. Keep workloads that require host networking in a separate configuration.
Examples
Before
services:
mongo:
image: mongo:latest
network_mode: "host"
After
services:
mongo:
image: mongo:latest
ports:
- "27017:27017"
Explanation:
- Before: MongoDB shares the host network.
- After:
27017:27017publishes the isolated network’s MongoDB port on all host interfaces by default. Set the intended binding address, firewall rules, and MongoDB authentication.