Container port binding is too broad

Limit published ports to the host interfaces the service requires.

Description

In ports, 7000:8000 publishes host port 7000 on all host interfaces by default. Connections are forwarded to port 8000 in the container.

Potential impact

A service intended only for the host may receive connection attempts from other machines.

Remediation

For host-only access, specify 127.0.0.1 in ports. For remote clients, bind to the intended host interface and restrict access with firewall rules. Configure public and internal services separately.

Examples

Both examples require the application to listen on container port 8000. With Docker Engine versions before 28.0.0, other hosts on the same layer-2 network may still reach a port published to loopback.

Before

yaml
services:
  webapp:
    image: sample/webapp:latest
    ports:
      - "7000:8000"

After

yaml
services:
  webapp:
    image: sample/webapp:latest
    ports:
      - "127.0.0.1:7000:8000"

Explanation:

  • Before: Omitting the host IP uses the default binding address.
  • After: The port is published at 127.0.0.1:7000 on the host. Manage application authentication and access between containers separately.

References