Chapter 2 · Lesson 14 of 20
Docker

Publish Docker container ports safely

Publishing a port with -p exposes it on every host address unless you bind it to 127.0.0.1, and Docker's own firewall rules can bypass UFW.

By the end you will

Bind a published container port to 127.0.0.1 and confirm it with ss.

Time
14 min
Level
Intermediate
Check
4 questions
You need
  • Docker installed
  • A reverse proxy on the host, or a test container

Use -p 127.0.0.1:8080:80 instead of -p 8080:80. Without an address, Docker listens on every interface, and Docker’s own firewall rules can let that traffic through even when UFW says the port is closed. Binding to loopback keeps the port private to the server.

How it works#

-p HOST:CONTAINER asks Docker to forward traffic from a port on the host to a port in the container. If you give no address, Docker listens on every host address: 0.0.0.0 for IPv4 and the wildcard [::] for IPv6. So -p 3000:3000 is reachable by anyone who can reach the server’s IP on that port.

UFW is a friendly front end for the host’s firewall, which is iptables or nftables underneath. Docker also writes its own iptables rules to do the port forwarding. Forwarded packets to a container take a different path through the firewall (the forwarding path, not the input path that UFW’s usual rules guard), and Docker’s rules accept them before UFW’s rules are consulted. That is why ufw deny 8080 can appear to do nothing. Docker’s documentation describes this behavior and the options for restricting it.

The reliable control is to decide the bind address yourself. 127.0.0.1:8080:80 listens only on loopback, so only processes on the server can connect. A reverse proxy on the host then takes public traffic on 80 and 443 and passes it to the loopback port. Containers on the same Docker network need no published port at all, because they talk to each other directly by service name.

Step 1: Know the two forms#

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

This publishes host port 8080 on all addresses, so anyone who can reach the server can reach it. Docker documents that published ports are handled by its own iptables rules and can bypass rules you set up in UFW.

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

This binds only to the loopback address, so the port is reachable from the server itself and nowhere else. What you should see from docker ps: the PORTS column shows 127.0.0.1:8080->80/tcp.

Step 2: Use loopback behind a reverse proxy#

If a reverse proxy on the host (nginx or Caddy installed on the host) serves the public traffic, bind the container to 127.0.0.1 and point the proxy at it. Only the proxy is exposed.

In Compose:

yaml
services:
  web:
    image: nginx
    ports:
      - "127.0.0.1:8080:80"

Apply with docker compose up -d. What you should see: Compose says the container was recreated. Existing containers must be recreated for the change to take effect. Quote the mapping in YAML so it is read as a string.

Check it worked#

bash
sudo ss -tlnp

-t is TCP, -l listening, -n numbers instead of names, -p the owning process. Look at the Local Address column for port 8080. You want 127.0.0.1:8080. If you see 0.0.0.0:8080 or [::]:8080, the port is open to every interface.

From another machine, curl http://203.0.113.10:8080 (your server’s real address) should time out or be refused, while curl http://127.0.0.1:8080 on the server should return the nginx page.

Edge cases and gotchas#

  • A UFW deny rule is not a fix for a published port. Docker’s forwarding rules can let traffic through anyway. Bind to 127.0.0.1 instead of relying on UFW for container ports.
  • IPv6 is a separate listener. A mapping without an address can show up as both 0.0.0.0:8080 and [::]:8080 in ss. Check both lines after a change.
  • Behind a proxy on another host. If the proxy runs on a different machine, loopback will not work. Bind to the private network address of the server (for example 10.0.0.5:8080:80) and restrict access at the cloud firewall.
  • Proxy runs in a container too. Put the proxy and the app on the same Docker network and skip publishing the app. Only publish ports 80 and 443 for the proxy.
  • Databases and admin panels. Never publish Postgres, Redis, MongoDB or similar to all addresses by habit. Compose files copied from tutorials often do. Drop the ports entry, or bind to 127.0.0.1 for local admin tools.
  • Port conflicts. Two containers cannot publish the same host port. The second one fails to start.
  • Changing the mapping does not change a running container. Recreate it with docker compose up -d or remove and rerun docker run.

Troubleshooting#

SymptomLikely causeFix
ss shows 0.0.0.0:8080 after your editThe old container is still runningRun docker compose up -d again, or docker rm -f web and rerun
port is already allocatedAnother container or process holds that host portFind it in ss -tlnp, pick another host port
bind: address already in useA host service such as nginx already listens thereStop it or choose a different port
Proxy reports connection refusedThe proxy targets the container address, or the container is downPoint it at 127.0.0.1:8080 and check docker ps
Port still reachable from the internetMapping has no address, or IPv6 listener remainsAdd 127.0.0.1: and check [::] lines in ss
Works on the server, not from your laptopCorrect for a loopback bindUse the reverse proxy, or an SSH tunnel for admin access
curl from the server to 127.0.0.1 failsContainer not started, or app listens on a different port insideCheck docker logs web and the container port
docker compose up -d says the port is in useTwo services map the same host portGive each a different host port, or drop the mapping
Phone or laptop cannot reach the app by IPYou bound to 127.0.0.1 on purposePut a reverse proxy in front, or tunnel with SSH

Real-world scenario#

Priya runs a small dashboard and a Postgres container on a VPS. She followed a tutorial that publishes 5432:5432. A week later her logs show login attempts from addresses she does not know. UFW allows only 22, 80 and 443, so she is surprised.

bash
sudo ss -tlnp | grep 5432

The output shows 0.0.0.0:5432. Docker’s forwarding rules bypassed UFW. She removes the mapping from the database, since the app reaches it by the service name on the Compose network, and binds the dashboard to loopback behind her reverse proxy:

yaml
ports:
  - "127.0.0.1:3000:3000"

After docker compose up -d, ss shows 127.0.0.1:3000 and nothing for 5432. She also rotates the database password, because the port was exposed for a week. The firewall basics are in set up a firewall with UFW, and the reason containers need no published port is in Docker networks explained.

Quick reference#

GoalMapping
Private to the server127.0.0.1:8080:80
Public on all interfaces8080:80
One specific private address10.0.0.5:8080:80
IPv6 loopback[::1]:8080:80
Check what is listeningsudo ss -tlnp
Apply a changed mappingdocker compose up -d
No publish at allomit ports, use the service name on the network

Security and safety notes#

  • Do not publish databases or admin interfaces on all addresses.
  • Before you flush or edit iptables by hand to fix this, remember that Docker recreates its rules on restart. Bind addresses in the mapping are the simplest durable fix.

Next#

Roll out new versions with update a container to a new image.

Check what you learned4 questions
1What does -p 8080:80 do to who can reach the port?
2In sudo ss -tlnp, which Local Address shows the port is private?
3You changed the port mapping in compose.yaml but ss still shows 0.0.0.0:8080. What do you do?
4Your app and its Postgres container share a Compose network. Which database setting is safest?

Frequently asked questions

Does UFW block published Docker ports?

Not reliably. Docker adds its own iptables rules, and traffic to published ports can skip the rules UFW manages. Docker's documentation warns about this.

When should I publish a port at all?

When something outside the host must reach the container directly. Containers on the same Docker network reach each other by service name with no published port.

Do I need to publish the database port so my app container can reach it?

No. Containers on the same Docker network connect by service name and the container's own port. Publishing the database port only makes it reachable from outside, which is usually what you do not want.

Does binding to 127.0.0.1 also cover IPv6?

127.0.0.1 is IPv4 loopback only. Check ss for [::]:PORT lines too, and use [::1] if you want to publish on IPv6 loopback.

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