What 0.0.0.0 means when publishing Docker ports
In a -p mapping, 0.0.0.0 means the host port listens on every IPv4 interface, while 127.0.0.1 limits it to the server itself.
- Updated Nov 27, 2026
- Markdown
Bind published ports to the right host address and confirm them with ss, docker port and curl.
- Docker Engine on a Linux server
- A container you can start with -p
A published port has two sides: an address and port on the host, and a port inside the container. The host side defaults to 0.0.0.0, which means “every IPv4 interface”, so -p 8080:80 is reachable from anywhere that can reach the server. Writing -p 127.0.0.1:8080:80 limits it to the server itself. Separately, the app inside the container must also listen on 0.0.0.0, not 127.0.0.1, or published traffic never reaches it.
How it works#
The -p flag has the form [host_address:]host_port:container_port. Docker listens on the host address and port you choose and forwards connections to the container’s private IP and port. If you leave out the host address, Docker uses 0.0.0.0 (all IPv4 interfaces) and usually adds a matching IPv6 listener.
Two different bind addresses are in play, and mixing them up causes most confusion:
- The host-side bind address, set in
-p. It decides who can connect to the server. - The in-container bind address, set in the application’s own config. It decides whether the app accepts the forwarded connection.
For the general idea of an address a service listens on, see bind address and port publishing.
Host-side bind addresses#
Publish on all interfaces (the default):
docker run -d --name web -p 8080:80 nginxPublish on loopback only:
docker run -d --name web -p 127.0.0.1:8080:80 nginxPublish on one specific interface, such as a private address of the server:
docker run -d --name web -p 10.0.0.5:8080:80 nginxIn Compose the same strings go in quotes under ports:
services:
web:
image: nginx
ports:
- "127.0.0.1:8080:80"The loopback form is the right choice for anything a reverse proxy on the same server will front. The proxy connects to 127.0.0.1:8080, and the container is not reachable directly from the internet. See what is a reverse proxy.
IPv6 and [::]#
The IPv6 wildcard is written [::]. When you publish without an address, docker ps and ss often show two listeners for the same mapping: one on 0.0.0.0 and one on [::]. To bind IPv6 loopback explicitly, use -p "[::1]:8080:80". Binding to 127.0.0.1 covers IPv4 only, so check that the IPv6 side is not left open when you rely on loopback. Whether IPv6 listeners appear depends on your Docker version and daemon settings, so confirm on your own server with ss.
Inside the container: listen on 0.0.0.0#
Docker forwards traffic to the container’s network interface (its address on the bridge), not to the container’s loopback. If the app binds to 127.0.0.1:3000 inside the container, it only accepts connections made from within the container itself. The published port then connects, but the connection is reset or hangs.
Most frameworks have a setting for this. Look for a host or bind option and set it to 0.0.0.0. A Node app might use app.listen(3000, "0.0.0.0"), and many dev servers need a flag such as --host 0.0.0.0. Note that listening on 0.0.0.0 inside the container is safe: the exposure to the outside world is controlled by the host-side -p address.
Published ports and the firewall#
Docker adds its own rules to the host firewall for published ports, so they can bypass the allow and deny rules you set in UFW. A port published as -p 8080:80 may be open to the internet even if UFW denies 8080. The full explanation and fixes are in publish container ports safely. The simplest defence is the loopback bind shown above, plus UFW for the ports that are not Docker’s.
Check it worked#
Show the mapping Docker made:
docker port webTypical output lists lines like 80/tcp -> 127.0.0.1:8080. Then look at what the host is really listening on:
ss -ltnFind the Local Address:Port column. 127.0.0.1:8080 means loopback only, 0.0.0.0:8080 means all IPv4 interfaces and [::]:8080 means all IPv6 interfaces. Finally test from the server and from another machine:
curl -I http://127.0.0.1:8080
curl -I http://203.0.113.10:8080With a loopback bind, the second command should fail to connect from outside. To see which process owns a port, use find what is using a port.
Edge cases and gotchas#
- Order matters: The address comes first.
-p 8080:127.0.0.1:80is not valid, while-p 127.0.0.1:8080:80is. - Loopback is per host:
127.0.0.1means the server, not your laptop. To test a loopback-bound port remotely, use an SSH tunnel. - Compose quoting: Quote port strings in YAML. Unquoted
80:80style values can be parsed as base-60 numbers by some parsers. - IPv6 left open: Binding IPv4 loopback does not close an IPv6 listener you added elsewhere, so check
ss -ltnfor[::]lines. - EXPOSE is not publishing: The
EXPOSEline in a Dockerfile only documents a port. Nothing is reachable from the host until-porportsis set. - Random host port:
-p 80or-Ppicks a random host port on all interfaces, which is easy to forget and easy to leave open.
Troubleshooting#
| Symptom | Likely cause | Fix |
|---|---|---|
| curl to the published port resets or hangs | App listens on 127.0.0.1 inside the container | Set the app’s host to 0.0.0.0 |
| Port reachable from the internet unexpectedly | Default 0.0.0.0 bind, firewall bypassed | Republish with 127.0.0.1:host:container |
| Cannot reach loopback-bound port from a laptop | 127.0.0.1 is the server’s loopback | Use a reverse proxy or an SSH tunnel |
| Container fails with address already in use | Host port already taken | Check with ss -ltnp and pick another host port |
| Mapping missing from docker port | Port not published | Add -p or ports and recreate the container |
Quick reference#
docker run -d -p 8080:80 nginx # all interfaces
docker run -d -p 127.0.0.1:8080:80 nginx # loopback only
docker run -d -p "[::1]:8080:80" nginx # IPv6 loopback
docker port web
ss -ltnRelated lessons#
- Publish container ports safely for how Docker interacts with the firewall.
- Docker bridge vs host networking for the mode where -p does not apply.
- Docker networks explained for how traffic moves on the bridge.
- What is a reverse proxy for the usual layer in front of loopback-bound apps.
- Find what is using a port for reading ss output.
Next#
Continue with DNS inside Docker.
Check what you learned
Frequently asked questions
What does 0.0.0.0:8080->80/tcp mean in docker ps?
It means host port 8080 is listening on all IPv4 interfaces and forwards to port 80 in the container. A second line with [::] shows the IPv6 equivalent.
How do I bind a Docker port to localhost only?
Prefix the host side with the loopback address, for example -p 127.0.0.1:8080:80, or in Compose ports: - "127.0.0.1:8080:80". Then put a reverse proxy in front for public access.
Why can't I reach my container app when I publish its port?
A common cause is the app listening on 127.0.0.1 inside the container. Make it listen on 0.0.0.0, usually through a host or bind setting in its config or a flag.
Does Docker's published port bypass UFW?
Yes, Docker writes its own firewall rules for published ports, so UFW allow and deny rules may not apply. Binding to 127.0.0.1 avoids the exposure.
Sources
Keep going: guides and tutorials
- Deploy a Docker Compose App Self-Hosted: Web, Redis and Rollback Deploy a Docker Compose app on your own server with Levelrail: a web service and Redis, a bad release that does not take traffic, and a compose rollback.
- Docker Container Restarting in a Loop: 5 Causes, Fixed Five real reasons a Docker container keeps restarting, each reproduced with its exit code, the command that finds it and the fix. Then the same crash on Levelrail.
- Deploy an Express App to a VPS With Docker and Health Checks Deploy a Node.js Express API to your own server with no Dockerfile. Covers the port rule, a health route, secrets, deploy failure codes and rollback.