Skip to content

Commit ba55802

Browse files
committed
fix(docker): apply fixed macs through network API
- Switch the Docker network restore path from endpoint driver options to the Docker network connect API when a fixed MAC is stored. - Before, rc.docker passed com.docker.network.endpoint.macaddress through docker network connect --driver-opt. - Docker 29.3.1 persisted that value in DriverOpts but still assigned a random endpoint MacAddress on macvlan networks. - That meant containers could reboot with random MACs even though MyMAC was stored in the template. - Now, fixed-MAC reconnects send EndpointConfig.MacAddress, with IPv4 and IPv6 addresses preserved through IPAMConfig, matching the working docker run endpoint form.
1 parent bab527f commit ba55802

1 file changed

Lines changed: 26 additions & 3 deletions

File tree

etc/rc.d/rc.docker

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,14 @@ netrestore_connect(){
245245
local MY_TT=$3
246246
local MY_MAC=$4
247247
local MY_IP=
248-
local MY_OPTS=
248+
local MY_IPV4=
249+
local MY_IPV6=
249250
local IP=
251+
local IPAM_JSON=
252+
local ENDPOINT_JSON=
253+
local CONNECT_JSON=
254+
local CODE=
255+
local BODY=
250256
local ENDPOINT_ID=
251257
local ENDPOINT_MAC=
252258
local OUT=
@@ -267,15 +273,16 @@ netrestore_connect(){
267273
for IP in ${MY_TT//;/ }; do
268274
[[ -n $IP ]] || continue
269275
if [[ $IP =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then
276+
MY_IPV4=$IP
270277
MY_IP="$MY_IP --ip $IP"
271278
elif [[ $IP =~ : ]]; then
279+
MY_IPV6=$IP
272280
MY_IP="$MY_IP --ip6 $IP"
273281
else
274282
log "skipping invalid stored IP for $CONTAINER on network $NETWORK: $IP"
275283
fi
276284
done
277285

278-
[[ -n $MY_MAC ]] && MY_OPTS="--driver-opt=com.docker.network.endpoint.macaddress=$MY_MAC"
279286
ENDPOINT_ID=$(docker inspect --format="{{with index .NetworkSettings.Networks \"$NETWORK\"}}{{.EndpointID}}{{end}}" "$CONTAINER" 2>/dev/null)
280287
if [[ -n $ENDPOINT_ID ]]; then
281288
[[ -n $MY_MAC ]] || return 0
@@ -288,8 +295,24 @@ netrestore_connect(){
288295
fi
289296
fi
290297

298+
if [[ -n $MY_MAC ]]; then
299+
[[ -n $MY_IPV4 ]] && IPAM_JSON="\"IPv4Address\":\"$MY_IPV4\""
300+
[[ -n $MY_IPV6 ]] && IPAM_JSON="${IPAM_JSON:+$IPAM_JSON,}\"IPv6Address\":\"$MY_IPV6\""
301+
ENDPOINT_JSON="\"MacAddress\":\"$MY_MAC\""
302+
[[ -n $IPAM_JSON ]] && ENDPOINT_JSON="\"IPAMConfig\":{$IPAM_JSON},$ENDPOINT_JSON"
303+
CONNECT_JSON="{\"Container\":\"$CONTAINER\",\"EndpointConfig\":{$ENDPOINT_JSON}}"
304+
OUT=$(curl --unix-socket /var/run/docker.sock -sS -w $'\n%{http_code}' -X POST -H "Content-Type: application/json" --data "$CONNECT_JSON" "http://localhost/networks/$NETWORK/connect" 2>&1)
305+
CODE=${OUT##*$'\n'}
306+
BODY=${OUT%$'\n'$CODE}
307+
if [[ $CODE != 2* ]]; then
308+
log "failed to connect $CONTAINER to network $NETWORK: $BODY"
309+
return 1
310+
fi
311+
return 0
312+
fi
313+
291314
log "connecting $CONTAINER to network $NETWORK"
292-
if ! OUT=$(docker network connect $MY_OPTS $MY_IP $NETWORK $CONTAINER 2>&1); then
315+
if ! OUT=$(docker network connect $MY_IP $NETWORK $CONTAINER 2>&1); then
293316
log "failed to connect $CONTAINER to network $NETWORK: $OUT"
294317
return 1
295318
fi

0 commit comments

Comments
 (0)