Bài gốc: From Internet to Container: A Deep Dive into Destiation NAT — tác giả BaconYao, lấy qua Notion public API.
Key Takeaway
Destination NAT (DNAT)
What is DNAT ?
In the previous chapter (09. From Container to Internet: A Deep Dive into Source NAT ), we discussed how, when a network packet travels from a container through the host to the external network,Source NAT usually modifies the packet’s source address (src).
Now, when the network packet returns from the external network back to the host, how does the system know which container it should be delivered to?
Destination NAT (DNAT) refers to modifying the destination IP or port of a packet when it enters the internal network (inbound traffic), allowing external requests to be directed to an internal host or service.
In other words, its purpose is:
“To route external requests to a specific internal service.”This mechanism is also known as
Port Forwarding or Reverse-proxy-style NAT.
DNAT vs SNAT
Where does Destination NAT take effect?

From the diagram on the left, we can see that Destination NAT is located in the
PREROUTING and OUTPUT chains.A closer inspection of the Netfilter hooks in Linux Kernel 6.16 also confirms this.The Hooks of DNAT in Linux Kernel
The Hooks of DNAT in Linux Kernel
Answer
Answer
-
PREROUTING for 『Forwarding-Type』 DNAT
This is the most common scenario:
📦 The traffic originates from an external source — the packet enters the network interface and then goes into
NF_INET_PRE_ROUTING. At this stage, DNAT intervenes in thePREROUTINGchain to modify the destination IP to10.0.0.5. After that, the kernel’s routing subsystem uses the new destination IP to route the packet to the correct internal host. ✅ Applicable scenario: The packet comes from an external source, and the system needs to forward it to an internal host. -
OUTPUT for 『Locally Generated』 DNAT
The packet is generated by a local process, and you want to change its destination before it leaves the host.
In this case, the packet does not pass throughPREROUTING(since it’s not “coming from outside”), so DNAT must be attached to theNF_INET_LOCAL_OUThook instead.With this rule, when you runcurl http://127.0.0.1:8080, the request is actually redirected to127.0.0.1:80. ✅ Applicable scenario: Packets generated by local applications also need to be redirected (loopback or internal redirection).
Experiment – Accessing a Local Container from Another Machine
Set Up the Experiment Environment
Two Hosts
In the upcoming experiment, we will need to perform a communication test between two machines.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:
Step 1. Spin up a Nginx Docker Container
Onmachine-1 (box-1), install the Nginx Docker container by running the following command:

To verify that the Nginx server has started correctly, after installation, run the following on As shown in the left figure, you should be able to access the Nginx container’s content from host (machine-1 (box-1)), which indicates that it is running properly.
machine-1 (box-1):Step 2. Monitor the packets on machine-1’s docker0 and eth0 interfaces

Next, to capture packet information, we need to monitor machine-1 (box-1) on both the
docker0 and eth0 interfaces.Step 3. Accessing the Nginx Server on the first machine from the second machine

Next, on machine-2 (box-2), use the following command to access the Nginx service on machine-1 (box-1):As shown in the left figure, the service can be accessed correctly.
Step 4. Observing DNAT in Action

From the lower part of the left figure, we can observe that a packet from machine-2 (box-2) passes through machine-1 (box-1)’s
eth0 interface with the following details:-
src: 172.16.0.3:59610 -
dst: 172.16.0.2:12345
docker0 interface, its destination is modified by DNAT as follows:-
src: 172.16.0.3:59610 -
dst: 172.17.0.2:80
docker0 bridge performs DNAT translation for us.Step 5. Check the rules in the nat table of iptables
Rule 1-
A PREROUTINGThe rule is added to the PREROUTING chain of the NAT table. PREROUTING is processed before the routing decision, i.e., before the kernel decides whether to deliver the packet locally or forward it. -
m addrtypeUses the addrtype match module (the kernel determines the address type, not just a string comparison). -
-dst-type LOCALMatches packets whose destination is considered 『LOCAL』 by the system, meaning the target IP belongs to one of the host’s interfaces (e.g., public IP, private IP, bridge IP, docker0 IP). -
j DOCKERIf the above conditions are met, jump to the DOCKER chain, where Docker places DNAT rules to redirect packets to the container.
Rule 2-
Packet is not from
docker0(i.e., comes from external network or the host itself) - Protocol is TCP and destination port is 12345
-
Packet is DNAT’ed to
172.17.0.2:80
172.17.0.2 on port 80. (This usually results from running Docker with -p 12345:80)Actual Packet Flow
-
An external client connects to the host’s (machine 1) public IP on port 12345 → the packet arrives at
eth0. -
The kernel processes NAT rules in PREROUTING:
dst-type LOCALcheck confirms that the destination IP is local (because ofdocker0bridge), so the match succeeds. -
The packet jumps to the DOCKER chain, which contains
p tcp --dport 12345 -j DNAT --to-destination 172.17.0.2:80. The packet is thus DNAT’ed to destination172.17.0.2:80. -
Routing is recalculated, and the packet is forwarded to
docker0→ enters FORWARD → delivered to the container.
-A OUTPUT ! -d 127.0.0.0/8 -m addrtype --dst-type LOCAL -j DOCKER handles it. Together, these rules support both external and local connections to the host IP.)
Conclusion
In this chapter, we explored the principles of Destination NAT and, through experimentation, observed that when a packet arrives at the host from an external network via theeth0 interface, the docker0 bridge performs DNAT, allowing the packet to reach the actual container.
Combined with what we previously learned about Source NAT, we now have a clear understanding of the complete journey of packets entering and leaving a container.