Skip to content

Commit 774ed6a

Browse files
committed
Add '--sysctl' option to 'network connect'
Equivalent to 'sysctl' in the extended 'run --network' syntax. Signed-off-by: Rob Murray <rob.murray@docker.com>
1 parent 1623e2b commit 774ed6a

3 files changed

Lines changed: 42 additions & 11 deletions

File tree

cli/command/network/connect.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type connectOptions struct {
2222
aliases []string
2323
linklocalips []string
2424
driverOpts []string
25+
sysctls []string
2526
}
2627

2728
func newConnectCommand(dockerCli command.Cli) *cobra.Command {
@@ -54,13 +55,14 @@ func newConnectCommand(dockerCli command.Cli) *cobra.Command {
5455
flags.StringSliceVar(&options.aliases, "alias", []string{}, "Add network-scoped alias for the container")
5556
flags.StringSliceVar(&options.linklocalips, "link-local-ip", []string{}, "Add a link-local address for the container")
5657
flags.StringSliceVar(&options.driverOpts, "driver-opt", []string{}, "driver options for the network")
58+
flags.StringSliceVar(&options.sysctls, "sysctl", []string{}, "short-form interface sysctl: net.X.Y.ethN.Z=V -> X.Y.Z=V")
5759
return cmd
5860
}
5961

6062
func runConnect(ctx context.Context, dockerCli command.Cli, options connectOptions) error {
6163
client := dockerCli.Client()
6264

63-
driverOpts, err := convertDriverOpt(options.driverOpts)
65+
driverOpts, err := convertDriverOpt(options.driverOpts, options.sysctls)
6466
if err != nil {
6567
return err
6668
}
@@ -78,7 +80,7 @@ func runConnect(ctx context.Context, dockerCli command.Cli, options connectOptio
7880
return client.NetworkConnect(ctx, options.network, options.container, epConfig)
7981
}
8082

81-
func convertDriverOpt(options []string) (map[string]string, error) {
83+
func convertDriverOpt(options []string, sysctls []string) (map[string]string, error) {
8284
driverOpt := make(map[string]string)
8385
for _, opt := range options {
8486
k, v, ok := strings.Cut(opt, "=")
@@ -89,5 +91,8 @@ func convertDriverOpt(options []string) (map[string]string, error) {
8991
}
9092
driverOpt[k] = strings.TrimSpace(v)
9193
}
94+
if len(sysctls) > 0 {
95+
driverOpt[opts.NetlabelEpSysctls] = strings.Join(sysctls, ",")
96+
}
9297
return driverOpt, nil
9398
}

cli/command/network/connect_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77

88
"github.com/docker/cli/internal/test"
9+
"github.com/docker/cli/opts"
910
"github.com/docker/docker/api/types/network"
1011
"github.com/pkg/errors"
1112
"gotest.tools/v3/assert"
@@ -52,7 +53,8 @@ func TestNetworkConnectWithFlags(t *testing.T) {
5253
Links: []string{"otherctr"},
5354
Aliases: []string{"poor-yorick"},
5455
DriverOpts: map[string]string{
55-
"adriveropt": "anoptval",
56+
"adriveropt": "anoptval",
57+
opts.NetlabelEpSysctls: "ipv6.conf.accept_ra=2,ipv6.conf.forwarding=1",
5658
},
5759
}
5860
cli := test.NewFakeCli(&fakeClient{
@@ -72,6 +74,8 @@ func TestNetworkConnectWithFlags(t *testing.T) {
7274
{"ip6", "fdef:f401:8da0:1234::5678"},
7375
{"link", "otherctr"},
7476
{"link-local-ip", "169.254.42.42"},
77+
{"sysctl", "ipv6.conf.accept_ra=2"},
78+
{"sysctl", "ipv6.conf.forwarding=1"},
7579
} {
7680
err := cmd.Flags().Set(opt.name, opt.value)
7781
assert.Check(t, err)

docs/reference/commandline/network_connect.md

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ Connect a container to a network
55

66
### Options
77

8-
| Name | Type | Default | Description |
9-
|:--------------------|:--------------|:--------|:-------------------------------------------|
10-
| [`--alias`](#alias) | `stringSlice` | | Add network-scoped alias for the container |
11-
| `--driver-opt` | `stringSlice` | | driver options for the network |
12-
| [`--ip`](#ip) | `string` | | IPv4 address (e.g., `172.30.100.104`) |
13-
| `--ip6` | `string` | | IPv6 address (e.g., `2001:db8::33`) |
14-
| [`--link`](#link) | `list` | | Add link to another container |
15-
| `--link-local-ip` | `stringSlice` | | Add a link-local address for the container |
8+
| Name | Type | Default | Description |
9+
|:----------------------|:--------------|:--------|:---------------------------------------------------------|
10+
| [`--alias`](#alias) | `stringSlice` | | Add network-scoped alias for the container |
11+
| `--driver-opt` | `stringSlice` | | driver options for the network |
12+
| [`--ip`](#ip) | `string` | | IPv4 address (e.g., `172.30.100.104`) |
13+
| `--ip6` | `string` | | IPv6 address (e.g., `2001:db8::33`) |
14+
| [`--link`](#link) | `list` | | Add link to another container |
15+
| `--link-local-ip` | `stringSlice` | | Add a link-local address for the container |
16+
| [`--sysctl`](#sysctl) | `stringSlice` | | short-form interface sysctl: net.X.Y.ethN.Z=V -> X.Y.Z=V |
1617

1718

1819
<!---MARKER_GEN_END-->
@@ -23,6 +24,14 @@ Connects a container to a network. You can connect a container by name
2324
or by ID. Once connected, the container can communicate with other containers in
2425
the same network.
2526

27+
The `--sysctl` option in `network connect` is restricted to sysctls that start with
28+
`net.ipv4.` and `net.ipv6.`. The `net.` prefix and the name of the interface must
29+
not be included. Internally, the sysctl options are added to `DriverOpts` with the
30+
key `com.docker.network.endpoint.sysctls`, which is where you'll find them in
31+
`docker inspect` output. Network drivers may restrict the sysctl settings that can
32+
be modified and, to protect the operation of the network, new restrictions may be
33+
added in the future.
34+
2635
## Examples
2736

2837
### Connect a running container to a network
@@ -64,6 +73,19 @@ being connected to.
6473
```console
6574
$ docker network connect --alias db --alias mysql multi-host-network container2
6675
```
76+
### <a name="sysctl"></a> Set a sysctl for a container's interface (--sysctl)
77+
78+
`--sysctl` sets a network sysctl on the interface created to connect to the
79+
network, only `net.ipv4.` and `net.ipv6.` settings can be modified. The
80+
interface name is assigned automatically, so it must be left out along with
81+
the `net.` prefix.
82+
83+
If the network interface created in the container is called `eth3`, this
84+
example is equivalent to `net.ipv4.conf.eth3.log_martians=1`.
85+
86+
```console
87+
$ docker network connect --sysctl ipv4.conf.log_martians=1 multi-host-network container2
88+
```
6789

6890
### Network implications of stopping, pausing, or restarting containers
6991

0 commit comments

Comments
 (0)