Shared host network namespace

Review the need for host networking and expose only required addresses and ports.

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

yaml
services:
  mongo:
    image: mongo:latest
    network_mode: "host"

After

yaml
services:
  mongo:
    image: mongo:latest
    ports:
      - "27017:27017"

Explanation:

  • Before: MongoDB shares the host network.
  • After: 27017:27017 publishes the isolated network’s MongoDB port on all host interfaces by default. Set the intended binding address, firewall rules, and MongoDB authentication.

References