Skip to content

Commit f9dc3d2

Browse files
committed
fix(docker): use configured gateways for VLAN networks
- Purpose: keep Docker custom networks on VLAN and secondary interfaces from losing their configured gateway during automatic network recreation. - Before: rc.docker only read the gateway from a default route on the interface, so VLANs without an interface-specific default route created Docker networks without --gateway. - Why that was a problem: Docker could claim the first subnet address as the macvlan/ipvlan gateway, colliding with real VLAN gateways such as 192.168.10.1 and breaking DHCP or static-IP containers. - What the new change accomplishes: automatic Docker network creation now falls back to the configured IPv4 or IPv6 gateway stored in network.ini when no live default route exists. - How it works: configured_gateway maps br/bond network names back to their eth network.ini section, resolves VLAN IDs to their indexed entries, and returns the matching GATEWAY or GATEWAY6 value before network create arguments are assembled.
1 parent aba2552 commit f9dc3d2

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

etc/rc.d/rc.docker

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,39 @@ network(){
210210
docker network ls --filter driver="$1" --format='{{.Name}}' 2>/dev/null | grep -P "^[a-z]+$2(\$|\.)" | tr '\n' ' '
211211
}
212212

213+
configured_gateway(){
214+
local NETWORK=$1
215+
local KEY=$2
216+
local BASE=${NETWORK%%.*}
217+
local VLAN=
218+
local SECTION
219+
220+
[[ $NETWORK == *.* ]] && VLAN=${NETWORK#*.}
221+
SECTION=${BASE/#br/eth}
222+
SECTION=${SECTION/#bond/eth}
223+
224+
awk -F'[=:"]+' -v section="[$SECTION]" -v vlan="$VLAN" -v key="$KEY" '
225+
$0 == section {
226+
inside = 1
227+
want = vlan == "" ? "0" : ""
228+
next
229+
}
230+
/^\[/ {
231+
inside = 0
232+
}
233+
inside && vlan != "" && $1 == "VLANID" && $3 == vlan {
234+
want = $2
235+
}
236+
inside && want != "" && $1 == key && $2 == want {
237+
value = $0
238+
sub(/^[^=]+="/, "", value)
239+
sub(/"$/, "", value)
240+
print value
241+
exit
242+
}
243+
' "$INI"
244+
}
245+
213246
# Is container running?
214247
container_running(){
215248
local CONTAINER
@@ -470,6 +503,7 @@ docker_network_start(){
470503
if [[ -n $IPV4 ]]; then
471504
SUBNET=$(ip -4 route show dev $NETWORK | sort | awk -v ORS=" " '$1 !~ /^default/ {print $1}' | sed 's/ $//')
472505
GATEWAY=$(ip -4 route show to default dev $NETWORK | awk '{print $3;exit}')
506+
[[ -n $GATEWAY ]] || GATEWAY=$(configured_gateway "$NETWORK" GATEWAY)
473507
SERVER=${IPV4%/*}
474508
DHCP=${NETWORK/./_}
475509
DHCP=DOCKER_DHCP_${DHCP^^}
@@ -481,6 +515,7 @@ docker_network_start(){
481515
if [[ -n $IPV6 ]]; then
482516
SUBNET6=$(ip -6 route show dev $NETWORK | sort | awk -v ORS=" " '$1 !~ /^(default|fe80)/ {print $1}' | sed 's/ $//')
483517
GATEWAY6=$(ip -6 route show to default dev $NETWORK | awk '{print $3;exit}')
518+
[[ -n $GATEWAY6 ]] || GATEWAY6=$(configured_gateway "$NETWORK" GATEWAY6)
484519
fi
485520
else
486521
# add user defined networks

0 commit comments

Comments
 (0)