Skip to content

Commit 0ab4470

Browse files
committed
Add --mac-address flag to network connect command
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 <[email protected]>
1 parent 874b831 commit 0ab4470

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

cli/command/network/connect.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ package network
22

33
import (
44
"context"
5+
"fmt"
6+
57
"errors"
8+
9+
610
"net"
711
"net/netip"
812
"strings"
@@ -21,6 +25,7 @@ type connectOptions struct {
2125
container string
2226
ipaddress net.IP // TODO(thaJeztah): we need a flag-type to handle netip.Addr directly
2327
ipv6address net.IP // TODO(thaJeztah): we need a flag-type to handle netip.Addr directly
28+
macAddress net.HardwareAddr
2429
links opts.ListOpts
2530
aliases []string
2631
linklocalips []net.IP // TODO(thaJeztah): we need a flag-type to handle []netip.Addr directly
@@ -32,6 +37,7 @@ func newConnectCommand(dockerCLI command.Cli) *cobra.Command {
3237
options := connectOptions{
3338
links: opts.NewListOpts(opts.ValidateLink),
3439
}
40+
var macStr string
3541

3642
cmd := &cobra.Command{
3743
Use: "connect [OPTIONS] NETWORK CONTAINER",
@@ -40,6 +46,15 @@ func newConnectCommand(dockerCLI command.Cli) *cobra.Command {
4046
RunE: func(cmd *cobra.Command, args []string) error {
4147
options.network = args[0]
4248
options.container = args[1]
49+
// Validate MAC address if provided
50+
51+
if macStr != "" {
52+
mac, err := net.ParseMAC(macStr)
53+
if err != nil {
54+
return fmt.Errorf("invalid MAC address: %q", macStr)
55+
}
56+
options.macAddress = mac
57+
}
4358
return runConnect(cmd.Context(), dockerCLI.Client(), options)
4459
},
4560
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
@@ -60,26 +75,34 @@ func newConnectCommand(dockerCLI command.Cli) *cobra.Command {
6075
flags.IPSliceVar(&options.linklocalips, "link-local-ip", nil, "Add a link-local address for the container")
6176
flags.StringSliceVar(&options.driverOpts, "driver-opt", []string{}, "driver options for the network")
6277
flags.IntVar(&options.gwPriority, "gw-priority", 0, "Highest gw-priority provides the default gateway. Accepts positive and negative values.")
78+
flags.StringVar(&macStr, "mac-address", "", "MAC address for the container on this network")
6379
return cmd
6480
}
6581

6682
func runConnect(ctx context.Context, apiClient client.NetworkAPIClient, options connectOptions) error {
6783
driverOpts, err := convertDriverOpt(options.driverOpts)
84+
6885
if err != nil {
6986
return err
7087
}
88+
89+
90+
91+
7192
_, err = apiClient.NetworkConnect(ctx, options.network, client.NetworkConnectOptions{
7293
Container: options.container,
7394
EndpointConfig: &network.EndpointSettings{
7495
IPAMConfig: &network.EndpointIPAMConfig{
7596
IPv4Address: toNetipAddr(options.ipaddress),
7697
IPv6Address: toNetipAddr(options.ipv6address),
7798
LinkLocalIPs: toNetipAddrSlice(options.linklocalips),
99+
78100
},
79101
Links: options.links.GetSlice(),
80102
Aliases: options.aliases,
81103
DriverOpts: driverOpts,
82104
GwPriority: options.gwPriority,
105+
MacAddress : network.HardwareAddr(options.macAddress),
83106
},
84107
})
85108
return err

0 commit comments

Comments
 (0)