<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Network Blog]]></title><description><![CDATA[Network Blog]]></description><link>https://network2889.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Fri, 20 Nov 2026 20:19:05 GMT</lastBuildDate><atom:link href="https://network2889.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[The Docker UFW Bypass: Why Your "Protected" Ports Are Wide Open to the Internet]]></title><description><![CDATA[How docker containers bypass ufw firewall completely.]]></description><link>https://network2889.hashnode.dev/the-docker-ufw-bypass-why-your-protected-ports-are-wide-open-to-the-internet</link><guid isPermaLink="true">https://network2889.hashnode.dev/the-docker-ufw-bypass-why-your-protected-ports-are-wide-open-to-the-internet</guid><category><![CDATA[Linux]]></category><category><![CDATA[Docker]]></category><category><![CDATA[networking]]></category><category><![CDATA[Security]]></category><category><![CDATA[Devops]]></category><category><![CDATA[netfilter]]></category><category><![CDATA[ufw]]></category><dc:creator><![CDATA[Kaustubh Awasthi]]></dc:creator><pubDate>Sun, 08 Nov 2026 12:50:34 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/67e97a84c4a8da59ee13ba3d/c30c279b-b889-4292-ac6d-3eab20a20fdd.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Suppose you enabled UFW, and set the default incoming policy to deny. Then you start a Redis container with <code>docker run -d -p 6379:6379 redis:alpine</code> and checked <code>ufw status</code> to verify that port 6379 wasn't open. From a remote laptop, <code>nc -zv &lt;public-ip&gt; 6379</code> connected instantly, and <code>redis-cli</code> responded with <code>PONG</code>.</p>
<p>So why does UFW says deny but our nc can connect to it? The answer is much simpler than you think. Its not like your ufw is not working, if you had ran the redis in linux itself instead of a container, you would have seen the result you want.</p>
<hr />
<h2>The Deceptive Reassurance of <code>ufw status</code></h2>
<p>Suppose you run the following commands:</p>
<pre><code class="language-bash">sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 22/tcp
sudo ufw enable
sudo ufw status verbose
</code></pre>
<p>And your result shows:</p>
<pre><code class="language-text">Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW IN    Anywhere
22/tcp (v6)                ALLOW IN    Anywhere (v6)
</code></pre>
<p>The status output confirms that incoming packets are dropped unless they target port 22. Next, you run a database container using standard documentation syntax.</p>
<pre><code class="language-bash">docker run -d --name redis-cache -p 6379:6379 redis:7-alpine
</code></pre>
<p>You run <code>ufw status</code> again, and the output doesn't change. Port 6379 doesn't appear in the list, leading you to believe the host's default deny policy protects Redis from outside traffic.</p>
<p>Now run a port check from a separate machine on the internet:</p>
<pre><code class="language-bash">$ nc -zv 203.0.113.45 6379
Connection to 203.0.113.45 6379 port [tcp/*] succeeded!

$ redis-cli -h 203.0.113.45 ping
PONG
</code></pre>
<p>The connection succeeds without a timeout or packet drop. Your Redis instance is directly accessible to every IP on the internet.</p>
<hr />
<h2>Netfilter Hooks: The Fork in the Road</h2>
<p>Linux doesn't evaluate firewall rules as a single sequential script. The Netfilter framework inside the kernel intercepts packets at five distinct hooks: <code>PREROUTING</code>, <code>INPUT</code>, <code>FORWARD</code>, <code>OUTPUT</code>, and <code>POSTROUTING</code>.</p>
<p>The critical event happens immediately after <code>PREROUTING</code>, where the kernel executes a routing decision. If a packet's destination IP matches an address owned by a local host interface, the kernel hands it to <code>INPUT</code>. If the destination IP belongs to a different network interface or container namespace, the kernel directs it to <code>FORWARD</code>.</p>
<p>A packet traverses either <code>INPUT</code> or <code>FORWARD</code>, but never both. This architectural split is the root reason why UFW lets your container for the whole world to access.</p>
<hr />
<h2>Tracing the Packet Trajectory</h2>
<p>The diagram below traces an incoming packet from the physical Wi-Fi interface (<code>wlan0</code>) to the container's <code>eth0</code> interface inside its network namespace:</p>
<img src="https://cdn.hashnode.com/uploads/covers/67e97a84c4a8da59ee13ba3d/c1302e83-6136-43fa-8aa1-9abe39ad5ccc.png" alt="Diagram" style="display:block;margin:0 auto" />

<p>External packets arrive on <code>wlan0</code> with destination <code>203.0.113.45:6379</code>. Docker's DNAT rule in <code>PREROUTING</code> rewrites the destination IP to <code>172.17.0.2</code> before the kernel makes its routing decision.</p>
<p>While the host owns the <code>docker0</code> bridge gateway IP (<code>172.17.0.1</code>), <code>172.17.0.2</code> is not assigned to any local interface inside the host's network namespace, it lives inside the container's isolated network namespace. Because the destination IP isn't local to the host, the kernel bypasses <code>INPUT</code> entirely and pushes the packet straight into <code>FORWARD</code> to be routed out through the <code>docker0</code> bridge.</p>
<p>Once through <code>FORWARD</code>, the packet crosses the <code>docker0</code> virtual switch into the host end of the virtual ethernet pair (<code>veth</code>). The other end of that virtual cable emerges inside the container's network namespace as <code>eth0</code>, where the Redis process receives it.</p>
<hr />
<h2>Why the FORWARD Chain Doesn't Drop It</h2>
<p>UFW's default <code>allow</code> and <code>deny</code> commands only write rules to <code>INPUT</code> and <code>OUTPUT</code> (<code>ufw-user-input</code> and <code>ufw-user-output</code>). They don't touch the <code>FORWARD</code> chain.</p>
<p>Docker populates the <code>FORWARD</code> chain with its own <code>ACCEPT</code> rules for mapped container ports. Because the packet skips <code>INPUT</code> and matches Docker's rules in <code>FORWARD</code>, UFW's incoming drop policies never evaluate it.</p>
<hr />
<h2>Three Ways to Fix the Leak</h2>
<p>Depending on whether your container needs external traffic or internal-only communication, you have three distinct methods to secure your setup.</p>
<h3>1. Bind to Localhost (The Simplest and Safest Fix)</h3>
<p>If a container only needs to talk to a local backend, reverse proxy, or application on the same host, specify <code>127.0.0.1</code> explicitly in the port flag:</p>
<pre><code class="language-bash">docker run -d --name redis-cache -p 127.0.0.1:6379:6379 redis:7-alpine
</code></pre>
<p>This changes the DNAT match rule so it only triggers when the packet destination is explicitly <code>127.0.0.1</code>. Packets coming from external interfaces like <code>wlan0</code> with a <code>127.0.0.0/8</code> destination are dropped as martian packets by the Linux kernel, preventing external reachability.</p>
<hr />
<h3>2. Filter External Traffic via <code>DOCKER-USER</code> Chain</h3>
<p>Docker creates the <code>DOCKER-USER</code> chain at the very top of <code>FORWARD</code> specifically for administrator firewall rules. Unlike the <code>DOCKER</code> chain, Docker never overwrites <code>DOCKER-USER</code> when containers restart.</p>
<pre><code class="language-bash"># 1. Allow a trusted internal LAN subnet (e.g., 192.168.1.0/24) to reach port 6379
sudo iptables -I DOCKER-USER -i wlan0 -s 192.168.1.0/24 -p tcp -m conntrack --ctorigdstport 6379 -j ACCEPT

# 2. Drop all other external traffic incoming on wlan0 targeting published port 6379
sudo iptables -A DOCKER-USER -i wlan0 -p tcp -m conntrack --ctorigdstport 6379 -j DROP
</code></pre>
<h3>3. Disabling Docker's iptables Management (Why It Causes Outages)</h3>
<p>You can tell Docker to stop touching iptables by adding <code>{"iptables": false}</code> to <code>/etc/docker/daemon.json</code>. While this stops Docker from bypassing UFW, it also breaks container outbound networking.</p>
<p>Without Docker managing iptables, the engine won't configure SNAT masquerading in <code>POSTROUTING</code>. Containers lose the ability to resolve external DNS or download packages unless you write and maintain all NAT and forwarding rules manually.</p>
<hr />
<h2>A 10-Second Diagnostic for Your Hosts</h2>
<p>Don't rely on <code>ufw status</code> to verify container isolation. Run this command on your servers to expose every port Docker is currently routing past your firewall:</p>
<pre><code class="language-bash">sudo iptables -t nat -nvL DOCKER | grep -E "DNAT.*to:"
</code></pre>
<p>If you see external interfaces forwarding traffic to container IPs on sensitive ports like 6379, recreate those containers bound to <code>127.0.0.1</code> immediately.</p>
]]></content:encoded></item></channel></rss>