Sub topic 3
Restricting a published port to one host IP
This chapter assumes you can run docker run, you know that -p HOST:CONTAINER publishes a port, and you know what localhost and a network interface are. You will learn how to choose which host address accepts connections for a published port.
What -p does when you give no IP
A command like -p 8080:80 does not name a host address. Docker then listens on all host interfaces, which is the wildcard address 0.0.0.0. Any machine that can reach your host on port 8080 can reach the container.
That is convenient on a laptop on a trusted network. It is risky on a server with a public address, or on a laptop on shared Wi-Fi.
Adding a host IP
The full form of the flag has three parts:
-p HOST_IP:HOST_PORT:CONTAINER_PORTTo accept connections only from the host itself, use the loopback address:
docker run -d --name web-local -p 127.0.0.1:8081:80 nginx
curl http://127.0.0.1:8081The curl command succeeds because it runs on the same machine. A request from another computer to your-host:8081 is refused, because nothing listens on the host's external address for that port.
Checking what Docker bound
Use docker port to see the binding:
docker port web-localThe output looks like 80/tcp -> 127.0.0.1:8081. If you had omitted the IP, it would show 0.0.0.0:8081. Newer Docker versions may also print a second line for the IPv6 wildcard, [::]:8081. This is the quickest way to confirm that a restriction took effect.
Choosing other addresses
You can bind to any address that belongs to the host.
127.0.0.1accepts local connections only. Use it for databases and admin tools that only other processes on the same host need.- A specific interface address, such as
192.168.1.10, accepts connections arriving on that interface only. Use it on a host with several network cards when only one network should reach the service. 0.0.0.0is the same as leaving the IP out.- An IPv6 address goes in square brackets, for example
-p [::1]:8081:80.
If you name an address the host does not have, docker run fails with an error such as cannot assign requested address.
Same port, different addresses
A host port is only taken for the address you bind it to. Two containers can both publish host port 8081 if they use different host IPs. A container that publishes 8081 with no IP uses the wildcard address, which overlaps every other address, so it conflicts with both.
A realistic example
A database that only local tools use should not listen on the network:
docker run -d --name db \
-p 127.0.0.1:5432:5432 \
-e POSTGRES_PASSWORD=example \
postgres:16Here -e POSTGRES_PASSWORD sets the password the official image needs to start. Later chapters cover -e in detail. A tool on the same host can connect to localhost:5432. Other machines cannot.
Common mistakes
| Mistake | Result | Fix |
|---|---|---|
| Writing `-p 8081:80:127.0.0.1` | Docker rejects the argument | Put the IP first: `127.0.0.1:8081:80` |
| Expecting `-p 8081:80` to be local-only | The port is open to the network | Add `127.0.0.1:` |
| Using `127.0.0.1` and then connecting from another machine | Connection refused | Bind to a network interface address, or use a reverse proxy |
| Reusing a host port already bound on the same address | `port is already allocated` | Pick another host port or another IP |
Exercise
You run docker run -d -p 127.0.0.1:9000:80 nginx. A coworker on the same network opens http://your-host:9000. What happens, and what single change makes the port reachable to them?
Show answer
The connection is refused, because Docker listens only on the loopback address, which other machines cannot reach. Remove the 127.0.0.1: prefix, giving -p 9000:80, or replace it with the host's network interface address. You can confirm the binding with docker port.