Bài gốc: Understanding the Relationship Between iptables, Docker, and Linux Bridge — tác giả BaconYao, lấy qua Notion public API.
Key Takeaway
Introduction
When we have a Linux machine connected to a network and want to perform certain actions on its packets, how should we proceed? First, we need to understand that in the Linux environment, network packets arriving from outside to the user application generally follow the flow shown in the diagram below.What is netfiler ?
-
Netfilteris a framework within the Linux kernel used for filtering, modifying, and forwarding network packets. - It provides a low-level infrastructure that allows hooks at different stages of packet processing (such as receiving, forwarding, and sending) within the kernel.
- Netfilter itself is built into the Linux kernel and offers underlying packet handling capabilities but does not directly interact with users.
-
The main functions of netfilter include:
- Packet filtering
- Network Address Translation (NAT)
- Packet modification (e.g., altering headers or marking packets)
- Packet forwarding or dropping
Reference: [netfilter/iptables project homepage
The netfilter.org project](https://www.netfilter.org/)What is iptables ?
-
iptablesis a user-space tool used to configure and manage rules within the netfilter framework, primarily operating at Layer 3 (the IP layer). - It allows system administrators to define how network packets entering or leaving a Linux system should be handled—such as allowing, dropping, modifying, or forwarding packets.
- iptables interacts with netfilter via a command-line interface, passing user-defined rules to the kernel’s netfilter modules.
-
iptables organizes its rules using tables and chains:
-
Tables: such as
filter(for filtering),nat(for network address translation), andmangle(for packet modification). -
Chains: such as
INPUT(incoming packets),OUTPUT(outgoing packets), andFORWARD(forwarded packets).
-
Tables: such as
5 Tables and 5 Chains in iptables
5 Tables
5 Tables
-
filter table- Purpose: Used for packet filtering to decide whether to allow or drop packets.
- Common Use Cases: Setting firewall rules, such as blocking traffic from specific IPs or ports.
- Default Table: If no table is specified, iptables commands operate on the filter table by default.
-
nat table- Purpose: Used for Network Address Translation (NAT), including Source NAT (SNAT) and Destination NAT (DNAT).
- Common Use Cases: IP masquerading, port forwarding, or NAT routing.
- Note: The nat table only processes packets when they first enter certain chains (like PREROUTING or POSTROUTING).
-
mangle table- Purpose: Used to modify packet header information, such as TTL (Time To Live), TOS (Type of Service), or marking packets.
- Common Use Cases: Advanced traffic control or Quality of Service (QoS) management.
- Feature: Rules in the mangle table are executed before other tables.
-
raw table- Purpose: Used to mark packets to bypass connection tracking, e.g., setting NOTRACK.
- Common Use Cases: For high-performance needs or to avoid connection tracking.
- Feature: The raw table has the highest priority, with rules processed before other tables.
-
security table-
Purpose:
- Handles packet processing related to Mandatory Access Control (MAC), especially for SELinux or other security modules.
- Allows filtering based on security contexts and is typically used in environments requiring higher security levels, such as enterprise servers or systems needing fine-grained security policies.
- Main functions include setting security markings on packets or interacting with SELinux security policies.
-
Common Use Cases:
- Integrating with SELinux for security context–based firewall rules.
- Applying rules that distinguish network traffic by security level or role, restricting traffic based on security contexts.
-
Notes:
- The security table is not enabled on all Linux systems and requires kernel support for SELinux or other MAC frameworks.
- It is generally processed after the filter and mangle tables, thus having lower priority than raw and mangle tables.
-
Purpose:
5 Chains
5 Chains
iptables defines five built-in chains within the netfilter framework, representing
hooks at different stages of packet processing:-
PREROUTING- Trigger time: When a packet just enters the system, before routing decisions are made.
- Applicable tables: nat, mangle, raw.
- Common uses: Destination NAT (DNAT), packet marking.
-
INPUT- Trigger time: When the packet’s destination is the local machine.
- Applicable tables: filter, mangle, security.
- Common uses: Filtering incoming packets, e.g., restricting access from certain sources.
-
FORWARD- Trigger time: When the packet is routed through the machine (the machine acts as a router).
- Applicable tables: filter, mangle.
- Common uses: Controlling traffic passing through the host, such as firewall rules on routers.
-
OUTPUT- Trigger time: When locally generated packets are about to be sent out.
- Applicable tables: filter, nat, mangle, raw, security.
- Common uses: Filtering or modifying packets sent by the local machine.
-
POSTROUTING- Trigger time: When the packet is about to leave the system, after routing decisions have been made.
- Applicable tables: nat, mangle, security.
- Common uses: Source NAT (SNAT), IP masquerading (MASQUERADE).
Further Understanding of the Filter Table
In most cases, the table we commonly use is the Filter Table, especially when using a Linux host as a router, where its intervention is necessary. When only using the filter table, we focus on three chains:INPUT, FORWARD, and OUTPUT. Packet filtering can be performed based on the following three scenarios:
-
The packet’s destination is the local machine (
INPUTchain). -
The packet is being forwarded through the machine (
FORWARDchain). -
The packet’s destination is another address, originating locally (
OUTPUTchain).
Useful Commands
Using the iptables-save Command
We can use the iptables-save command to display the contents of different tables. For example, using the command: $ iptables-save -t filter we can view the contents of the filter table.
From the output above, we can see that the filter table contains three chains: INPUT, FORWARD, and OUTPUT.
-
:INPUT ACCEPT [0:0]- This chain handles packets coming into the local machine.
-
The default policy is
ACCEPT, meaning all incoming packets are accepted by default.
-
:FORWARD ACCEPT [0:0]- This chain handles packets being forwarded through the machine (e.g., when the machine acts as a router).
-
The default policy is also
ACCEPT, allowing forwarded packets.
-
:OUTPUT ACCEPT [0:0]- This chain handles packets originating from the local machine.
-
The default policy is
ACCEPT.
[0:0] after each chain represents [packet count : byte count].
-
Packet count (
0here) indicates how many packets have been processed by this chain. -
Byte count (
0here) indicates the total size in bytes of all processed packets.
Using the iptables Command
Besides using iptables-save, we can also use the iptables command with other options to view the rules. For example, the following command shows the filter table with detailed information: $ iptables -L -t filter -v
Change the FORWARD chain policy in the filter table from ACCEPT to DROP
Examples of Adding and Deleting Rules
Adding
Deleting
Linux Bridge and iptables
Quick Review of Linux Bridge
Abridge is a virtual network interface in the Linux kernel that implements Layer 2 (L2) network bridging, meaning:
- It connects multiple network interfaces together like a physical switch.
- It forwards packets between interfaces based on MAC addresses (similar to a switch).
-
Common use cases include:
-
Virtual networks for containers (e.g., Docker’s
docker0bridge) - Bridge mode networking for KVM/libvirt virtual machines
- Kubernetes networking (e.g., CNI plugins) for pod-to-pod communication
-
Virtual networks for containers (e.g., Docker’s
What’s the relationship between bridge and iptables?
By default, bridges only handle Layer 2 (MAC address) packet forwarding, so bridged traffic (e.g., between VMs or containers) does not necessarily pass through iptables. If you want to use iptables firewalls to control:- Container traffic passing through bridges (like Docker bridges)
- Virtual machine bridge interface traffic
net.bridge.bridge-nf-call-iptables. Otherwise, packets won’t enter iptables at all!
Linux kernel Config - net.bridge.bridge-nf-call-iptables
-
When
net.bridge.bridge-nf-call-iptablesis set to1(enabled), it means:- Packets received by the bridge interface will enter the iptables processing flow (especially the FORWARD chain).
- Rules set in iptables can control packets passing through the bridge.
-
Conversely, when
net.bridge.bridge-nf-call-iptablesis set to0(disabled):- Packets passing through the bridge will not be processed by iptables.
- In other words, your firewall rules will have no effect on traffic passing through the bridge.
Real-World Scenario
Docker Default Bridge (docker0)
-
By default, Docker uses the
docker0bridge (as shown in Experiment 1 of this note). -
If you want to control container inbound/outbound traffic with iptables (e.g., blocking certain ports), you need packets on the bridge to pass through iptables. This means
net.bridge.bridge-nf-call-iptablesmust be set to1.
KVM Virtual Machines Using Bridge to Connect to External Network
-
Suppose you connect a physical NIC and virtual NICs together using a bridge (e.g.,
br0). - Traffic between virtual machines flows through the bridge.
-
If
bridge-nf-call-iptablesis not enabled, iptables will not see this traffic at all and therefore cannot filter it.
Experiment 1 – What Docker Did
Set Up the Experiment Environment
Install Docker
- Install following the official guide: Ubuntu
-
After installation, the Docker service is not enabled by default, so we need to enable it using the following command:
Comparison of iptables Before and After Installing Docker
The following diagram provides a simple comparison of the iptables contents to help us understand what changes Docker has made.
iptales after installing Docker
iptales after installing Docker
Main Differences (Brief Summary)
1. The default policy of the FORWARD chain changes from ACCEPT to DROP
1. The default policy of the FORWARD chain changes from ACCEPT to DROP
- Impact: This is the most significant change. The FORWARD chain, which originally allowed all forwarding traffic, now defaults to dropping all forwarded packets unless explicitly permitted by a rule. This may block non-Docker forwarding traffic, such as LXD, VPN, or other bridged networks.
- Reason: To ensure the security of container networks, Docker sets the default policy of the FORWARD chain to DROP and only allows traffic that matches its specific rules to pass through.
2. Several custom chains have been added
2. Several custom chains have been added
-
Docker added the following custom chains:
- DOCKER: Handles Docker container-specific forwarding rules.
-
DOCKER-BRIDGE: Manages rules related to the bridge network (
docker0).- Docker uses the
docker0bridge interface, and traffic passes through the FORWARD chain becausenet.bridge.bridge-nf-call-iptables=1.
- Docker uses the
-
DOCKER-CT: Deals with connection tracking (conntrack) related rules.
- Rules allow traffic originating from containers (
i docker0 -j ACCEPT) and established response traffic (via DOCKER-CT), while restricting direct external access to containers unless NAT rules are present.
- Rules allow traffic originating from containers (
- DOCKER-FORWARD: Coordinates Docker’s forwarding rules.
- DOCKER-ISOLATION-STAGE-1 and DOCKER-ISOLATION-STAGE-2: Implement isolation between containers.
- DOCKER-USER: Allows users to insert custom rules that take precedence over Docker’s own rules.
- These chains form the core mechanism Docker uses to manage container networking, including NAT, port mapping, and isolation.
3. Jump rules added to the FORWARD chain
3. Jump rules added to the FORWARD chain
- A FORWARD -j DOCKER-USER: All forwarded traffic first goes to the DOCKER-USER chain, allowing users to insert custom rules.
- A FORWARD -j DOCKER-FORWARD: If no match is found in DOCKER-USER, traffic proceeds to the DOCKER-FORWARD chain, where Docker’s own rules handle it.
4. No changes were made to the INPUT and OUTPUT chains
4. No changes were made to the INPUT and OUTPUT chains
- The default policies for the INPUT and OUTPUT chains remain ACCEPT, and no new rules have been added. This indicates that Docker only affects forwarding traffic (the FORWARD chain) and does not directly impact the host’s incoming or outgoing traffic.
Experiment 2 – The Impact of iptables and Custom Bridges
Set Up the Experiment Environment
Setup Network Namespaces and Bridge
command
command
Host details, and the right image shows the information for network namespaces ns1 and ns2).


Let’s begin the experiment!
Scenario 1. FORWARD chain is DROP

In Experiment 1, we clearly saw that Docker changes the iptables
FORWARD chain policy to DROP and handles packet forwarding through the docker0 bridge. Because of this, our self-created br0 bridge cannot function properly.-
Step 1: Use the command
$ iptables-save -t filterto check the current iptables configuration. -
Step 2: Monitor packets passing through the Host’s
br0bridge interface. -
Step 3: From
ns1, pingns2.
-
The
pingcommandfails. -
ICMP echo reqest packets pass through the
br0interface, but no ICMP echo reply packets are received.

First, we change the FORWARD chain policy from DROP to ACCEPT using the command:
$ iptables -t filter -P FORWARD ACCEPT .Then, we ping from ns1 to ns2 again. This time, we observe that the two network namespaces can communicate successfully.Scenario 2. FORWARD chain is DROP, disable net.bridge.bridge-nf-call-iptables

From Scenario 1, we learned that the state of the iptables FORWARD chain affects packet forwarding through the
br0 bridge. Is there a way to decouple iptables from our custom bridge so that the FORWARD chain doesn’t affect br0?Yes, we can achieve this by adjusting the kernel parameter net.bridge.bridge-nf-call-iptables so that packets passing through the br0 interface are not processed by iptables’ filter table.-
Step 1: Use
$ iptables-save -t filterto check the current iptables configuration. -
Step 2: Set
net.bridge.bridge-nf-call-iptablesto0to prevent packets going through thebr0bridge from being filtered by iptables. -
Step 3: Ping from
ns1tons2.
Scenario 3. FORWARD chain is DROP, enable net.bridge.bridge-nf-call-iptables, add custom rule
Besides the methods used in Scenario 1 and Scenario 2, are there any other ways to allow communication between ns1 and ns2 when the FORWARD chain policy is set to DROP and
net.bridge.bridge-nf-call-iptables is enabled (set to 1)?Yes, we can add custom FORWARD rules ourselves!-
Step 1,Add a FORWARD rule using the command
sudo iptables -t filter -I FORWARD 1 -j ACCEPTThis accepts all incoming packets and allows forwarding. The rule is inserted at the top, taking precedence over all Docker rules. -
Step 2,Set
net.bridge.bridge-nf-call-iptablesto1to ensure iptables affects packets passing through thebr0bridge. -
Step 3,Check the current iptables configuration with
iptables-save -t filter -
Step 4,Ping from
ns1tons2.

By the way, we can use the command
$ iptables-save -t filter -c to view the packet and byte counters for each rule.