fix: allow binding containers on different IPs to the same port#4800
Open
yankay wants to merge 1 commit intocontainerd:mainfrom
Open
fix: allow binding containers on different IPs to the same port#4800yankay wants to merge 1 commit intocontainerd:mainfrom
yankay wants to merge 1 commit intocontainerd:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a regression in nerdctl’s Linux port-conflict detection so containers can bind the same host port on different host IPs without being incorrectly rejected as “already allocated” when CNI portmap iptables rules are present.
Changes:
- Read CNI portmap per-container DNAT sub-chains (
CNI-DN-*) from iptables to capture destination-IP-specific bindings (with fallback to the parent chain when sub-chains don’t exist). - Extend iptables rule parsing to return
{IP, Port}pairs (supporting both--dportsand--dport, and extracting-d <ip>). - Update used-port filtering to only treat a port as conflicting when IPs overlap (wildcards conflict with everything; specific IPs only with same IP or wildcard).
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| pkg/portutil/port_allocate_linux.go | Filters iptables-derived used ports by requested IP vs rule IP (wildcard-aware). |
| pkg/portutil/iptable/iptables.go | Changes parser output from ports-only to {IP, Port} and parses -d + --dport/--dports. |
| pkg/portutil/iptable/iptables_test.go | Updates unit tests for the new {IP, Port} parsing behavior and adds sub-chain cases. |
| pkg/portutil/iptable/iptables_linux.go | Reads CNI-DN-* sub-chains (and falls back to CNI-HOSTPORT-DNAT when absent). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // sub-chains and does not include destination IP filtering. | ||
| chains, err := ipt.ListChains(table) | ||
| if err != nil { | ||
| return nil, nil |
Comment on lines
+53
to
+57
| hasDNChains = true | ||
| subRules, err := ipt.List(table, chain) | ||
| if err != nil { | ||
| continue | ||
| } |
pkg/portutil/port_allocate_linux.go
Outdated
Comment on lines
+128
to
+133
| requestedIsWildcard := ip == "" || ip == "0.0.0.0" || ip == "::" | ||
| for _, rule := range portRules { | ||
| ruleIsWildcard := rule.IP == "" || rule.IP == "0.0.0.0" || rule.IP == "::" | ||
| if requestedIsWildcard || ruleIsWildcard || rule.IP == ip { | ||
| usedPort[rule.Port] = true | ||
| } |
10ba730 to
41db171
Compare
Fixes containerd#4786 Signed-off-by: Kay Yan <kay.yan@daocloud.io> Made-with: Cursor
41db171 to
68bdc22
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #4786
The port conflict check introduced in PR #4097 (commit 04f836d) incorrectly reports "port is already allocated" when binding containers to different host IPs but the same port number. For example:
Root cause
ReadIPTablesonly read theCNI-HOSTPORT-DNATparent chain, which dispatches by port but does not include-d <ip>destination IP filtering. The actual IP filtering is inCNI-DN-*sub-chains.ParseIPTableRulesonly extracted port numbers from--dportsand completely ignored destination IPs.This made every iptables-managed port appear globally allocated regardless of which host IP it was bound to.
Fix
ReadIPTables: ReadCNI-DN-*sub-chains (which contain DNAT rules with both-d <ip>and--dport), falling back to the parent chain if no sub-chains exist.ParseIPTableRules: ReturnPortRule{IP, Port}instead of just port numbers. Parse both--dport(sub-chains) and--dports(parent chain), and extract-d <ip>destination.getUsedPorts: Filter iptables ports by IP overlap — wildcard addresses ("",0.0.0.0,::) conflict with everything; specific IPs only conflict with the same IP or wildcards.Test plan
go test ./pkg/portutil/...)127.0.0.1/127.0.0.2/127.0.0.3:8880all start successfully0.0.0.0wildcard correctly conflicts with existing bindingscurl(HTTP 200)