Chapter 2 · Lesson 17 of 20
Docker

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.

By the end you will

Bind published ports to the right host address and confirm them with ss, docker port and curl.

Time
12 min
Level
Intermediate
Check
4 questions
You need
  • 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):

bash
docker run -d --name web -p 8080:80 nginx

Publish on loopback only:

bash
docker run -d --name web -p 127.0.0.1:8080:80 nginx

Publish on one specific interface, such as a private address of the server:

bash
docker run -d --name web -p 10.0.0.5:8080:80 nginx

In Compose the same strings go in quotes under ports:

yaml
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:

bash
docker port web

Typical output lists lines like 80/tcp -> 127.0.0.1:8080. Then look at what the host is really listening on:

bash
ss -ltn

Find 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:

bash
curl -I http://127.0.0.1:8080
curl -I http://203.0.113.10:8080

With 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:80 is not valid, while -p 127.0.0.1:8080:80 is.
  • Loopback is per host: 127.0.0.1 means 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:80 style 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 -ltn for [::] lines.
  • EXPOSE is not publishing: The EXPOSE line in a Dockerfile only documents a port. Nothing is reachable from the host until -p or ports is set.
  • Random host port: -p 80 or -P picks a random host port on all interfaces, which is easy to forget and easy to leave open.

Troubleshooting#

SymptomLikely causeFix
curl to the published port resets or hangsApp listens on 127.0.0.1 inside the containerSet the app’s host to 0.0.0.0
Port reachable from the internet unexpectedlyDefault 0.0.0.0 bind, firewall bypassedRepublish with 127.0.0.1:host:container
Cannot reach loopback-bound port from a laptop127.0.0.1 is the server’s loopbackUse a reverse proxy or an SSH tunnel
Container fails with address already in useHost port already takenCheck with ss -ltnp and pick another host port
Mapping missing from docker portPort not publishedAdd -p or ports and recreate the container

Quick reference#

bash
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 -ltn

Next#

Continue with DNS inside Docker.

Check what you learned4 questions
1What does docker run -p 8080:80 nginx bind on the host by default?
2How do you publish a port so only the server itself can reach it?
3Your app inside the container listens on 127.0.0.1:3000 and you publish -p 3000:3000. What happens?
4Which command shows the host side mapping for a running container named web?

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

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios