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.
- Updated Nov 27, 2026
- Markdown
Bind a published container port to 127.0.0.1 and confirm it with ss.
- 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#
docker run -d --name web -p 8080:80 nginxThis 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.
docker run -d --name web -p 127.0.0.1:8080:80 nginxThis 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:
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#
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.1instead 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:8080and[::]:8080inss. 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
portsentry, or bind to127.0.0.1for 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 -dor remove and rerundocker run.
Troubleshooting#
| Symptom | Likely cause | Fix |
|---|---|---|
ss shows 0.0.0.0:8080 after your edit | The old container is still running | Run docker compose up -d again, or docker rm -f web and rerun |
port is already allocated | Another container or process holds that host port | Find it in ss -tlnp, pick another host port |
bind: address already in use | A host service such as nginx already listens there | Stop it or choose a different port |
| Proxy reports connection refused | The proxy targets the container address, or the container is down | Point it at 127.0.0.1:8080 and check docker ps |
| Port still reachable from the internet | Mapping has no address, or IPv6 listener remains | Add 127.0.0.1: and check [::] lines in ss |
| Works on the server, not from your laptop | Correct for a loopback bind | Use the reverse proxy, or an SSH tunnel for admin access |
curl from the server to 127.0.0.1 fails | Container not started, or app listens on a different port inside | Check docker logs web and the container port |
docker compose up -d says the port is in use | Two services map the same host port | Give each a different host port, or drop the mapping |
| Phone or laptop cannot reach the app by IP | You bound to 127.0.0.1 on purpose | Put 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.
sudo ss -tlnp | grep 5432The 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:
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#
| Goal | Mapping |
|---|---|
| Private to the server | 127.0.0.1:8080:80 |
| Public on all interfaces | 8080:80 |
| One specific private address | 10.0.0.5:8080:80 |
| IPv6 loopback | [::1]:8080:80 |
| Check what is listening | sudo ss -tlnp |
| Apply a changed mapping | docker compose up -d |
| No publish at all | omit 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.
Related lessons#
- Set up a firewall with UFW: the host firewall Docker can bypass
- What is a reverse proxy: the public front door for loopback services
- Docker networks explained: container-to-container traffic without published ports
- Find what is using a port: read ss output
Next#
Roll out new versions with update a container to a new image.
Check what you learned
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
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.