Skip to content

Conversation

@ahmedharabi
Copy link

Add --mac-address support to docker network connect

Closes #51796

This change adds support for specifying a MAC address when connecting an existing container to a network using docker network connect, bringing feature parity with docker run --network.

The flag follows the same behavior as existing network-related options such as --ip and --ip6. The MAC address is parsed and validated client-side, then forwarded to the daemon via EndpointSettings.MacAddress.

This is particularly useful for macvlan networks, where explicit MAC address assignment is a common requirement.

Note: This change requires a companion update in moby/moby for the daemon to actually apply the MAC address during network connection.


What I did

  • Added a --mac-address flag to docker network connect
  • Implemented MAC address parsing and validation
  • Passed the MAC address through EndpointSettings.MacAddress to the daemon

How I did it

  • Introduced a new CLI flag consistent with existing network flags
  • Validated input using net.ParseMAC
  • Converted the value to network.HardwareAddr before sending it to the API
  • Ensured invalid MAC addresses are rejected before making the API call

How to verify it

docker network create -d macvlan \
  --subnet=192.168.70.0/24 \
  --gateway=192.168.70.1 \
  -o parent=<interface> test-macvlan

docker run -d --name mysql3 mysql:8.0

docker network connect \
  --mac-address 02:42:ac:70:00:28 \
  test-macvlan mysql3

docker inspect mysql3 | jq '.[0].NetworkSettings.Networks."test-macvlan".MacAddress'

This adds support for specifying a MAC address when connecting
a container to a network, particularly useful for macvlan networks.

The flag follows the same pattern as existing network options like
--ip and --ip6, parsing and validating the MAC address before
sending it to the daemon via the EndpointSettings.MacAddress field.

Note: This requires a companion change in moby/moby to have the
daemon actually apply the MAC address during network conne

Signed-off-by: ahmedharabi <harabiahmed88@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant