Skip to content
Blog

Binding Ports to Specific Host Interfaces and IP Addresses in Docker

A deep dive into binding Docker container ports to specific host IP addresses like 127.0.0.1, managing public vs private interfaces, and preventing dangerous default wildcard exposures.

Published on • November 17, 2026

AI Assistant

When publishing ports using standard -p 8080:80 syntax, Docker binds host port 8080 to all host network interfaces (0.0.0.0 for IPv4 and :: for IPv6). This wildcard default makes the service reachable across every network interface on the host—including public internet interfaces.

To prevent accidental public exposure of databases, administrative panels, and internal microservices, you must explicitly bind ports to specific host IP addresses.


1. The Danger of Default Wildcard Binding (0.0.0.0)

Running:

docker run -d --name redis-db -p 6379:6379 redis:alpine

Causes Docker to listen on 0.0.0.0:6379. If your host server is attached to a public cloud network (such as AWS EC2 or DigitalOcean Droplets) without an external firewall, your Redis instance is immediately accessible to the public internet!

Furthermore, Docker manipulates iptables directly, overriding host UFW or firewalld rules in many standard Linux configurations.


2. Binding Ports to Localhost (127.0.0.1)

For services that should only be accessible from the local host machine (such as databases accessed via SSH tunnels or reverse proxies running on the same host), specify 127.0.0.1 as the host IP:

# Bind Redis exclusively to loopback interface (localhost)
docker run -d --name internal-redis \
  -p 127.0.0.1:6379:6379 \
  redis:alpine

Verification:

Verify that the port is bound strictly to 127.0.0.1 using docker port or netstat / ss:

docker port internal-redis

Output:

6379/tcp -> 127.0.0.1:6379

External requests arriving on public host network cards (e.g., eth0 with IP 203.0.113.5) will be rejected by the kernel.


3. Binding to Specific Private Network Interfaces

In enterprise infrastructure, servers often feature multiple network interfaces:

  • eth0 (Public WAN): 203.0.113.15
  • eth1 (Private LAN/VPC): 10.0.1.50

To restrict container access exclusively to internal VPC traffic across the private network interface (10.0.1.50), specify the private interface IP:

docker run -d --name vpc-api \
  -p 10.0.1.50:8080:80 \
  my-api-image:latest

Traffic sent to http://203.0.113.15:8080 from the public internet will fail to connect, while internal traffic from http://10.0.1.50:8080 succeeds.


4. Configuring Global Daemon IP Binding Defaults

Rather than relying on developers to explicitly prepend 127.0.0.1 to every docker run command, you can configure the Docker daemon to use 127.0.0.1 as its default binding address system-wide.

Edit /etc/docker/daemon.json on the host machine:

{
  "ip": "127.0.0.1"
}

Reload and restart the Docker daemon service:

sudo systemctl restart docker

Now, issuing docker run -p 8080:80 ... automatically binds to 127.0.0.1:8080:80 by default.


5. Summary and Best Practices

Takeaway: Avoid default 0.0.0.0 wildcard port publishing. Explicitly restrict container interfaces by specifying host IP prefixes like -p 127.0.0.1:8080:80 for local access or -p 10.0.x.x:8080:80 for private network interfaces, or set "ip": "127.0.0.1" inside /etc/docker/daemon.json.


Sources