<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>firewall &#8211; Aveshost Blog</title>
	<atom:link href="https://blog.aveshost.com/tag/firewall/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.aveshost.com</link>
	<description>Your Blueprint for Online Success</description>
	<lastBuildDate>Thu, 19 Nov 2026 17:45:26 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.5</generator>

<image>
	<url>https://blog.aveshost.com/wp-content/uploads/2026/01/logo-150x150.png</url>
	<title>firewall &#8211; Aveshost Blog</title>
	<link>https://blog.aveshost.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Docker and UFW: How to Stop Containers Exposing Unexpected Ports</title>
		<link>https://blog.aveshost.com/docker-ufw-unexpected-ports/</link>
					<comments>https://blog.aveshost.com/docker-ufw-unexpected-ports/#respond</comments>
		
		<dc:creator><![CDATA[James Wilson]]></dc:creator>
		<pubDate>Thu, 19 Nov 2026 17:44:55 +0000</pubDate>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Docker]]></category>
		<category><![CDATA[firewall]]></category>
		<category><![CDATA[Linux VPS]]></category>
		<category><![CDATA[UFW]]></category>
		<guid isPermaLink="false">https://blog.aveshost.com/?p=6013</guid>

					<description><![CDATA[Learn why Docker-published ports can bypass UFW and how to reduce exposure with private networks, loopback bindings, backend-aware rules and external testing.]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph"><strong>Docker and UFW</strong> need separate attention on a Linux VPS. A container published with <code>8080:80</code> can remain reachable even when UFW denies incoming port 8080. The most reliable first fix is to remove unnecessary host port mappings, bind host-only services to loopback, and filter any deliberately public Docker traffic at the correct layer.</p>



<p class="wp-block-paragraph">Our recommended workflow is to inventory exposure, choose who should reach each service, change its mapping, and test from outside the VPS. A green “active” status in UFW is not proof that a container port is private.</p>



<p class="wp-block-paragraph">This guide covers rootful Docker Engine with bridge networking on an Ubuntu VPS. Host networking, Swarm ingress, rootless Docker and routed container networks require their own review. Before changing firewall rules, keep your current SSH session open, confirm recovery-console access and preserve your existing configuration.</p>



<div class="wp-block-rank-math-toc-block" id="rank-math-toc"><h2>Table of Contents</h2><nav><div><div><a href="#why-bypass">1. Why Docker-published ports can bypass UFW</a></div><div><a href="#audit">2. Audit mappings, network mode and firewall backend</a></div><div><a href="#choose-access">3. Decide which services actually need a public port</a></div><div><a href="#loopback">4. Bind host-only services to loopback</a></div><div><a href="#apply">5. Apply Compose changes without losing persistent data</a></div><div><a href="#iptables">6. Use DOCKER-USER for deliberate iptables filtering</a></div><div><a href="#nftables">7. Treat native nftables and host networking separately</a></div><div><a href="#verify">8. Verify Docker and UFW from outside the VPS</a></div><div><a href="#faq">Frequently asked questions</a></div></div></nav></div>



<h2 id="why-bypass" class="wp-block-heading">1. Why Docker-published ports can bypass UFW</h2>



<p class="wp-block-paragraph">UFW commonly controls traffic addressed to services on the host through the INPUT path. Docker’s bridge port publishing uses destination NAT and forwarding rules. Container-bound packets can therefore take a different path before the usual host-service rules apply. Docker documents this <a href="https://docs.docker.com/engine/network/packet-filtering-firewalls/" rel="noopener">UFW incompatibility</a>.</p>



<p class="wp-block-paragraph">For example, a host-installed SSH service on port 22 and a container mapped from host port 8080 to container port 80 need different checks. Adding <code>ufw deny 8080/tcp</code> does not establish that the Docker mapping is blocked.</p>



<p class="wp-block-paragraph">Keep UFW for appropriate host-service policy. Review Docker exposure alongside it. Do not set Docker’s <code>iptables</code> or <code>ip6tables</code> options to false as a shortcut: this can break masquerading and container networking without delivering the intended protection.</p>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="1536" height="1024" src="https://blog.aveshost.com/wp-content/uploads/2026/11/docker-ufw-traffic-paths.webp" alt="Docker and UFW traffic paths comparing host INPUT filtering with Docker DNAT and FORWARD traffic to a container." class="wp-image-6015" srcset="https://blog.aveshost.com/wp-content/uploads/2026/11/docker-ufw-traffic-paths.webp 1536w, https://blog.aveshost.com/wp-content/uploads/2026/11/docker-ufw-traffic-paths-300x200.webp 300w, https://blog.aveshost.com/wp-content/uploads/2026/11/docker-ufw-traffic-paths-1024x683.webp 1024w, https://blog.aveshost.com/wp-content/uploads/2026/11/docker-ufw-traffic-paths-768x512.webp 768w" sizes="(max-width: 1536px) 100vw, 1536px" /></figure>



<h2 id="audit" class="wp-block-heading">2. Audit mappings, network mode and firewall backend</h2>



<p class="wp-block-paragraph">Start with read-only checks. Replace <code>web</code> with the container you are investigating:</p>



<pre class="wp-block-code"><code>sudo docker version
sudo docker ps --format 'table {{.Names}}	{{.Ports}}'
sudo docker port web
sudo docker inspect web --format '{{.HostConfig.NetworkMode}}'
sudo ufw status verbose
ip -br address</code></pre>



<p class="wp-block-paragraph">In <code>docker ps</code>, <code>0.0.0.0:8080-&gt;80/tcp</code> indicates an IPv4 wildcard mapping; <code>[::]:8080-&gt;80/tcp</code> indicates an IPv6 wildcard mapping. A plain <code>80/tcp</code> entry is an exposed container port, not by itself a host publication. The <a href="https://docs.docker.com/reference/cli/docker/container/port/" rel="noopener">docker port command</a> lists actual mappings.</p>



<p class="wp-block-paragraph">Check the effective Compose configuration with <code>sudo docker compose config</code> from the project directory. Include the same override files and environment inputs used during deployment. Its rendered output can contain secrets; inspect it locally and redact it before sharing.</p>



<p class="wp-block-paragraph">For backend diagnosis, inspect <code>sudo systemctl cat docker</code> and, if present, <code>/etc/docker/daemon.json</code>. Then inspect <code>sudo iptables -S DOCKER-USER</code> and, where available, <code>sudo nft list ruleset</code>. Do not infer Docker’s native backend solely from <code>iptables --version</code>: an <code>nf_tables</code> version string can mean the iptables compatibility frontend.</p>



<h2 id="choose-access" class="wp-block-heading">3. Decide which services actually need a public port</h2>



<p class="wp-block-paragraph">Build a short access list before editing anything. A website, its database and its administration screen rarely need identical reachability. Record the owner and intended clients for every published port.</p>



<p class="wp-block-paragraph">For container-to-container traffic, attach the services to an appropriate shared Docker network and use service names, such as <code>db:5432</code>. Remove the database’s host <code>ports:</code> mapping when no external client needs it. Compose networking provides service discovery; <code>expose:</code> is not a firewall or a host-port publication.</p>



<p class="wp-block-paragraph">Limit shared-network membership too: removing a host mapping does not prevent peer containers on that network from reaching the service. Docker’s <a href="https://docs.docker.com/reference/compose-file/networks/" rel="noopener">Compose networking reference</a> describes network membership.</p>



<p class="wp-block-paragraph">For remote maintenance, prefer an authenticated VPN or SSH tunnel instead of a public database or dashboard port. A deliberately public service still needs authentication, updates and application-level access controls.</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>Client</th><th>Suitable arrangement</th><th>Public host mapping</th></tr></thead><tbody><tr><td>Another container</td><td>Shared network; service-name connection</td><td>Usually none</td></tr><tr><td>Reverse proxy installed on host</td><td>Backend bound to loopback</td><td>Backend stays local</td></tr><tr><td>Visitors to a website</td><td>Public proxy with private backends</td><td>Only required web ports</td></tr><tr><td>Administrator</td><td>VPN or SSH tunnel</td><td>Avoid direct public admin port</td></tr></tbody></table></figure>



<h2 id="loopback" class="wp-block-heading">4. Bind host-only services to loopback</h2>



<p class="wp-block-paragraph">If a reverse proxy installed directly on the VPS needs to reach a container, change the mapping from <code>8080:80</code> to <code>127.0.0.1:8080:80</code>. Merge this fragment into the existing service, preserving its image, volumes, environment and other settings:</p>



<pre class="wp-block-code"><code>services:
  app:
    ports:
      - "127.0.0.1:8080:80"</code></pre>



<p class="wp-block-paragraph">Point the host proxy at <code>http://127.0.0.1:8080</code>. The application inside the container normally still listens on its container interface, often <code>0.0.0.0:80</code>. Binding the host side and binding inside the container are different settings.</p>



<p class="wp-block-paragraph">If the reverse proxy is also a container, its <code>127.0.0.1</code> refers to itself. Put proxy and application on a suitable shared network, use an upstream such as <code>app:80</code>, and remove the application’s host mapping.</p>



<p class="wp-block-paragraph">Without a host address, published mappings normally bind all host addresses. Review IPv6 mappings separately. Docker also warns that releases older than 28.0.0 have a same-L2 reachability issue for localhost-published ports; upgrade before relying on that boundary. See <a href="https://docs.docker.com/engine/network/port-publishing/" rel="noopener">Docker’s port-publishing documentation</a>.</p>



<p class="wp-block-paragraph">For a temporary SSH tunnel, run this on your workstation, replacing the username and VPS address:</p>



<pre class="wp-block-code"><code>ssh -N -L 127.0.0.1:18080:127.0.0.1:8080 user@VPS_ADDRESS</code></pre>



<p class="wp-block-paragraph">Then visit <code>http://127.0.0.1:18080</code> locally. The SSH account must be permitted to forward connections. This example follows the <a href="https://man.openbsd.org/ssh#L" rel="noopener">OpenSSH local-forwarding syntax</a>; the tunnel ends when that SSH connection ends.</p>



<figure class="wp-block-image size-full"><img decoding="async" width="1536" height="1024" src="https://blog.aveshost.com/wp-content/uploads/2026/11/docker-minimal-port-exposure.webp" alt="Four Docker access patterns: private container networks, host loopback, public web proxy and VPN or SSH administration." class="wp-image-6016" srcset="https://blog.aveshost.com/wp-content/uploads/2026/11/docker-minimal-port-exposure.webp 1536w, https://blog.aveshost.com/wp-content/uploads/2026/11/docker-minimal-port-exposure-300x200.webp 300w, https://blog.aveshost.com/wp-content/uploads/2026/11/docker-minimal-port-exposure-1024x683.webp 1024w, https://blog.aveshost.com/wp-content/uploads/2026/11/docker-minimal-port-exposure-768x512.webp 768w" sizes="(max-width: 1536px) 100vw, 1536px" /></figure>



<h2 id="apply" class="wp-block-heading">5. Apply Compose changes without losing persistent data</h2>



<p class="wp-block-paragraph">Before recreation, verify backups and persistent mounts. Files stored only in a container’s writable layer can be lost when it is replaced. Arrange a maintenance window for services that cannot tolerate interruption.</p>



<p class="wp-block-paragraph">Check the merged configuration for both the intended loopback mapping and any old wildcard mapping. Compose override files or a deployment platform can reintroduce ports. Edit the configuration that actually owns the deployment.</p>



<pre class="wp-block-code"><code>sudo docker compose config
sudo docker compose up -d --no-deps app
sudo docker compose ps
sudo docker port YOUR_APP_CONTAINER</code></pre>



<p class="wp-block-paragraph">Replace the service and container names with your own. <code>compose up</code> recreates a service when its configuration changes; a plain restart does not apply a changed port mapping. See the <a href="https://docs.docker.com/reference/cli/docker/compose/up/" rel="noopener">Compose up reference</a>.</p>



<p class="wp-block-paragraph">Check the website, health endpoint and database connection immediately. If rollback is needed, restore the reviewed configuration and redeploy that service. Do not use <code>docker compose down -v</code> for a port change; deleting volumes is unnecessary and can destroy persistent data.</p>



<h2 id="iptables" class="wp-block-heading">6. Use DOCKER-USER for deliberate iptables filtering</h2>



<p class="wp-block-paragraph">For Docker’s iptables backend, user restrictions belong in <code>DOCKER-USER</code>, before Docker’s forwarding decisions. DNAT has already happened there, so matching the original published host port requires conntrack. Its use can add processing overhead. Follow <a href="https://docs.docker.com/engine/network/firewall-iptables/" rel="noopener">Docker’s iptables guidance</a>.</p>



<p class="wp-block-paragraph">Here is a narrow IPv4/TCP example for a standard VPS that is not routing unrelated networks. It drops traffic entering <code>ens3</code> to original destination port 8080 unless its source is <code>198.51.100.10</code>. Replace the interface and documentation-only source address first; it covers that port on all destination addresses reached through the interface:</p>



<pre class="wp-block-code"><code>sudo iptables -I DOCKER-USER 1 -i ens3 -p tcp ! -s 198.51.100.10/32 -m conntrack --ctdir ORIGINAL --ctorigdstport 8080 -j DROP</code></pre>



<p class="wp-block-paragraph">The permitted source continues through existing rules; this does not override other restrictions. ORIGINAL direction avoids dropping return packets for container-initiated connections. Inspect rule counters and test with fresh connections from permitted and denied clients:</p>



<pre class="wp-block-code"><code>sudo iptables -vnL DOCKER-USER --line-numbers</code></pre>



<p class="wp-block-paragraph">To remove exactly the example rule, use the same arguments with <code>-D</code>:</p>



<pre class="wp-block-code"><code>sudo iptables -D DOCKER-USER -i ens3 -p tcp ! -s 198.51.100.10/32 -m conntrack --ctdir ORIGINAL --ctorigdstport 8080 -j DROP</code></pre>



<p class="wp-block-paragraph">This rule covers neither UDP nor IPv6. Before relying on it, review those paths and your existing rule order. Make persistence explicit using your firewall-management process, ensure the chain exists before restoring the rule, and test after Docker restarts and a reboot. Do not flush Docker’s chains.</p>



<h2 id="nftables" class="wp-block-heading">7. Treat native nftables and host networking separately</h2>



<p class="wp-block-paragraph">Docker’s native nftables backend has no <code>DOCKER-USER</code> chain. Custom filtering belongs in separate tables and base chains at suitable hook priorities; do not edit Docker-owned tables. Docker describes support introduced in Engine 29.0.0 as experimental at the time of writing. Consult its <a href="https://docs.docker.com/engine/network/firewall-nftables/" rel="noopener">nftables documentation</a> for the installed release.</p>



<p class="wp-block-paragraph">A chain left over from a previous iptables configuration does not prove it will keep protecting traffic after a backend switch or reboot. Do not switch backends merely to make a copied command run. Use loopback or remove publication while a qualified administrator designs the required rules.</p>



<p class="wp-block-paragraph">With <code>network_mode: host</code>, containers share the host network namespace and port-publishing settings do not provide isolation. Audit application listeners and host firewall policy directly; see the <a href="https://docs.docker.com/engine/network/drivers/host/" rel="noopener">host-network driver documentation</a>. If a service unexpectedly uses host mode, changing its <code>ports:</code> list will not solve the exposure.</p>



<h2 id="verify" class="wp-block-heading">8. Verify Docker and UFW from outside the VPS</h2>



<p class="wp-block-paragraph">From an external workstation you control, test the VPS’s actual IPv4 and IPv6 addresses. For the example TCP port, using OpenBSD-style netcat:</p>



<pre class="wp-block-code"><code>nc -4 -vz -w 3 VPS_IPV4 8080
nc -6 -vz -w 3 VPS_IPV6 8080</code></pre>



<p class="wp-block-paragraph">A private backend should fail externally while its intended local proxy or tunnel still works. For a source allowlist, test from both permitted and denied networks. Test public HTTPS and open a second SSH session before closing the first. A test from the VPS itself does not exercise the same incoming path.</p>



<p class="wp-block-paragraph">A timeout alone cannot identify which layer blocked traffic. Compare mapping output, rule counters and application health; upstream firewalls can mask a host misconfiguration. Repeat after redeployment, firewall reload, Docker restart and a planned reboot. Review UDP services separately rather than treating a TCP result as proof.</p>



<p class="wp-block-paragraph">Keep UFW’s host rules aligned with the services you intentionally run, using the <a href="https://ubuntu.com/server/docs/how-to/security/firewalls/" rel="noopener">Ubuntu firewall guide</a>. If an upstream VPS firewall is available, use it as another layer, with separate IPv4 and IPv6 policy.</p>



<p class="wp-block-paragraph">For a deployment you maintain yourself, explore our <a href="https://www.aveshost.com/self-managed-vps-hosting" rel="noopener">self-managed VPS hosting</a>. If you need help reviewing an Aveshost server, <a href="https://www.aveshost.com/support" rel="noopener">contact our support team</a> with redacted port mappings and the intended access list. Our <a href="https://blog.aveshost.com/docker-disk-space-full/">Docker storage troubleshooting guide</a> covers another common VPS maintenance problem.</p>



<h2 id="faq" class="wp-block-heading">Frequently asked questions</h2>


<div id="rank-math-faq" class="rank-math-block">
<div class="rank-math-list ">
<div id="faq-question-096838292548430796504dd2f68da213" class="rank-math-list-item">
<h3 class="rank-math-question ">Does ufw deny 8080 guarantee a Docker container is blocked?</h3>
<div class="rank-math-answer ">

<p>No. A bridge-network published port can take Docker’s forwarding path instead of the host INPUT path. Remove or narrow the mapping, or apply backend-appropriate filtering, then verify externally.</p>

</div>
</div>
<div id="faq-question-ca7e291839414cb09f8af6b9e65af158" class="rank-math-list-item">
<h3 class="rank-math-question ">Does EXPOSE in a Dockerfile publish a public port?</h3>
<div class="rank-math-answer ">

<p>EXPOSE documents intended container ports. Host publication is a separate operation. Audit explicit publish flags, publish-all flags, Compose ports and deployment-platform settings.</p>

</div>
</div>
<div id="faq-question-1b3a26b573174e8d84105bd18d2fe5ba" class="rank-math-list-item">
<h3 class="rank-math-question ">Can another container reach a service bound to host loopback?</h3>
<div class="rank-math-answer ">

<p>A host loopback binding restricts that host publication. Peer containers sharing the application network may still reach its container port directly. Control network membership and application authentication too.</p>

</div>
</div>
<div id="faq-question-c5962ad8eb25410a9e52f4e0c4fc9301" class="rank-math-list-item">
<h3 class="rank-math-question ">Should I disable Docker iptables integration to fix UFW?</h3>
<div class="rank-math-answer ">

<p>That is not a general fix. Docker relies on generated networking rules, and disabling them requires a complete replacement design. Prefer reducing published ports and applying filtering for the active backend.</p>

</div>
</div>
<div id="faq-question-86e877aeedd248e39fad306bddf8426e" class="rank-math-list-item">
<h3 class="rank-math-question ">Why can I still reach the old port after editing Compose?</h3>
<div class="rank-math-answer ">

<p>Check whether the container was recreated, whether an override file retained the mapping, and whether another container or host process owns the port. Compare the rendered configuration with actual mappings and repeat an external test.</p>

</div>
</div>
</div>
</div>]]></content:encoded>
					
					<wfw:commentRss>https://blog.aveshost.com/docker-ufw-unexpected-ports/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
