Skip to contentcloudvix
cloudvix
GuideServers & hosting

Docker ignores UFW: how to close published ports on Ubuntu 24.04

Denys DunaievskyiFounder, Cloudvix
  • 16 min read
  • Published
  • LevelIntermediate
Guides
On this page

Key takeaways

  • Docker adds its own rules to the kernel firewall.
  • Anything with 0.0.0.0 or [::] is open to the network, whatever UFW says, unless something else filters it; test from outside.
  • A port with a host_ip line is bound to that address; a port without one is open to everyone.

UFW blocks a port, Docker publishes it anyway, and the container is reachable from the whole internet. This is not a bug in either tool: Docker writes its own rules into the kernel firewall, and traffic to a published port never meets the rules UFW manages. This guide reproduces the problem on a test container, closes it by choosing the address a port is published on, and shows how to check from outside that it is closed.

Before you start

You need an Ubuntu 24.04 server with Docker, as in the Docker install guide, and UFW switched on, as in the UFW guide. The commands start a throw-away nginx container called leak-test, and the last step removes it. If you already run containers, the list in the second step shows which of them are open to the network.

  • an Ubuntu 24.04 server with Docker and UFW active
  • a user with sudo
  • a second computer, or your laptop, to test from outside

The steps

See the problem

The first command shows that UFW denies incoming connections by default. The second starts a web server container and publishes its port 80 as port 8080 on every address of the server. Then, from your own computer, ask for the page. If the answer is HTTP/1.1 200 OK, the port is open although no UFW rule allows it.

Docker adds its own rules to the kernel firewall. A packet for a published port is redirected to the container and forwarded, so it takes a different path than traffic for the server itself, and UFW's rules, which guard the server itself, never see it. That is also why ufw deny 8080 would change nothing.

On the serverbash
sudo ufw status verbose
sudo docker run -d --rm --name leak-test -p 8080:80 nginx:stable
On your computerbash
curl -sI -m 5 http://SERVER_IP:8080 | head -n 1

List what is published

docker ps shows the ports of each container. 0.0.0.0:8080->80/tcp means that host port 8080 is open on every IPv4 address, and [::]:8080->80/tcp means the same for IPv6. 127.0.0.1:8080->80/tcp means that only the server itself can reach it. Anything with 0.0.0.0 or [::] is open to the network, whatever UFW says, unless something else filters it; test from outside.

On the serverbash
sudo docker ps --format 'table {{.Names}}\t{{.Ports}}'

Publish on localhost only

The fix is to put an address in front of the port: -p 127.0.0.1:8080:80. The container is then reachable from the server itself, for example by a web server or reverse proxy on the same machine, and from nowhere else. The commands remove the test container, start it again with the address and list the ports; the line must now begin with 127.0.0.1:.

On the serverbash
sudo docker rm -f leak-test
sudo docker run -d --rm --name leak-test -p 127.0.0.1:8080:80 nginx:stable
sudo docker ps --format 'table {{.Names}}\t{{.Ports}}'

The same in a Compose file

In a Compose file the address is part of the port: "127.0.0.1:8080:80". Containers that only talk to other containers need no ports: entry at all; they reach each other over the Compose network by service name, as cache does in the example. The second command prints the settings Compose will use. A port with a host_ip line is bound to that address; a port without one is open to everyone. Run the second command in the project folder on the server.

On the server · compose.yamlyaml
services:
  web:
    image: nginx:stable
    ports:
      - "127.0.0.1:8080:80"
    restart: unless-stopped
  cache:
    image: redis:7
    restart: unless-stopped
On the serverbash
sudo docker compose config | grep -E 'host_ip|published|target'
Tip

Put the quotes around the port mapping. Without them, YAML can read a value such as 22:22 as a number in base 60.

Make localhost the default

"ip": "127.0.0.1" in /etc/docker/daemon.json sets the address that Docker uses when a port mapping names none. The log-driver and log-opts lines are the log limit from the Docker install guide. dockerd --validate checks the file before the restart. Containers keep the address they were created with, so recreate them afterwards: docker compose up -d --force-recreate. Ports that have to stay public, such as 80 and 443 of a reverse proxy, now need the address spelled out in the mapping: "0.0.0.0:443:443" for IPv4 and, if the port must also answer on IPv6, "[::]:443:443" as a second line. Run docker ps first, so that you know which ports are published, and again after recreating the containers.

On the server · /etc/docker/daemon.jsonjson
sudo tee /etc/docker/daemon.json > /dev/null <<'EOF'
{
  "ip": "127.0.0.1",
  "log-driver": "json-file",
  "log-opts": { "max-size": "10m", "max-file": "3" }
}
EOF
sudo dockerd --validate --config-file=/etc/docker/daemon.json
sudo systemctl restart docker
sudo docker ps
Warning

The command replaces the whole file. If yours has other settings, merge them instead of pasting over them. Restarting Docker stops containers that have no restart policy. After the change, a port without an address is local only: spell out the address of each port that has to stay public before you recreate the containers.

Publish on a VPN address

A service that you want to reach from your own computer but not from the internet can be published on the address of a VPN interface. With the setup of the WireGuard guide, the server has the address 10.8.0.1 inside the tunnel, so -p 10.8.0.1:8081:80 makes the port reachable only for devices that are connected. The address has to exist when the container starts, so Docker must start after the VPN. The second block makes that permanent: a drop-in file for the Docker service (what sudo systemctl edit docker would create, without an editor) makes Docker wait for wg-quick@wg0 at every boot. Every device that is connected to the VPN can reach this port, so connect only devices you trust.

On the serverbash
sudo docker run -d --rm --name vpn-test -p 10.8.0.1:8081:80 nginx:stable
sudo docker ps --format 'table {{.Names}}\t{{.Ports}}'
On the server · /etc/systemd/system/docker.service.d/wait-for-vpn.confbash
sudo mkdir -p /etc/systemd/system/docker.service.d
sudo tee /etc/systemd/system/docker.service.d/wait-for-vpn.conf > /dev/null <<'EOF'
[Unit]
After=wg-quick@wg0.service
Wants=wg-quick@wg0.service
EOF
sudo systemctl daemon-reload

Check from outside

Run these on your own computer, not on the server. The first asks for the IPv4 port, the second for the IPv6 port; put the server's addresses in. After the fix, both must fail: curl ends with Connection timed out or Connection refused instead of a status code. A status line such as 200 means the port is still open. If the container was started before the change, recreate it first.

On your computerbash
curl -sS -m 5 -o /dev/null -w '%{http_code}\n' http://SERVER_IP:8080
curl -6 -sS -m 5 -o /dev/null -w '%{http_code}\n' 'http://[SERVER_IPV6]:8080'

Make it a habit

This one-line check lists the containers that are published on every address. No output means that nothing is open to the network by way of Docker. Run it after each deployment, or let an assistant run it, because a new ports: line is easy to add and easy to overlook.

On the serverbash
sudo docker ps --format '{{.Names}}\t{{.Ports}}' | grep -E '0\.0\.0\.0|\[::\]'

Remove the test containers

This deletes both test containers. If you did not start the VPN one, Docker says that it does not exist, which is fine.

On the serverbash
sudo docker rm -f leak-test vpn-test

If something goes wrong

bind: cannot assign requested address

The address in your port mapping does not exist on the server. For a VPN address, the VPN is not up yet. Check with ip -brief address and start the VPN first.

Docker does not start after editing daemon.json

The file has a mistake, most often a missing comma or a duplicate key. Run sudo dockerd --validate --config-file=/etc/docker/daemon.json to see it and sudo journalctl -u docker --since "10 minutes ago" --no-pager for the log. Fix the file and start Docker again.

The proxy gets "connection refused" from the container

A reverse proxy that runs in a container itself sees 127.0.0.1 as its own container, not the server. Put both in the same Compose project and let the proxy use the service name, for example http://web:80; only the proxy needs a published port.

The port is still open after the change

The old container is still running with the old mapping. Run sudo docker compose up -d --force-recreate, or remove the container and start it again, and list the ports once more.

Questions people ask

Does ufw deny 8080 help?

Not for a port that Docker publishes. The rule sits in the part of the firewall that guards the server itself; published ports are forwarded to the container on a different path. Change the address of the port mapping instead.

Is this a Docker bug?

No. The Docker documentation describes that Docker manages the firewall rules for the containers it publishes and that this bypasses the rules of tools such as UFW. Newer Docker versions change some details, so read the current documentation for your version.

What about a container that uses host networking?

With network_mode: host there is no port mapping: the program opens its ports on the server itself, so UFW applies to them as to any other program. In that case, allow only the ports you want in UFW.

Where to go next

UFW is the other half of this: see the UFW guide for the rules for the server itself. To publish a service only inside a VPN, set up WireGuard. If Docker is not installed yet, start with the Docker guide.

About the author

Denys Dunaievskyi

Founder, Cloudvix

Runs Cloudvix and is responsible for the guides published here.

Related guides

Your server is a few clicks away.

Choose the plan and the system in one order form; the location too, when there is more than one.

Create server