Skip to content

Commit 7b9a9aa

Browse files
authored
Merge pull request #656 from liubin/add-docs-for-net
add docs for using CNI plugins with nerdctl
2 parents dcb5001 + 696aa4f commit 7b9a9aa

File tree

4 files changed

+66
-4
lines changed

4 files changed

+66
-4
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ nerdctl is a **non-core** sub-project of containerd.
2727

2828
### Basic usage
2929

30-
To run a container with the default CNI network (10.4.0.0/24):
30+
To run a container with the default `bridge` CNI network (10.4.0.0/24):
3131
```console
3232
# nerdctl run -it --rm alpine
3333
```
@@ -1298,6 +1298,7 @@ Configuration guide:
12981298
Basic features:
12991299
- [`./docs/compose.md`](./docs/compose.md): Compose
13001300
- [`./docs/rootless.md`](./docs/rootless.md): Rootless mode
1301+
- [`./docs/cni.md`](./docs/cni.md): CNI for containers network
13011302

13021303
Advanced features:
13031304
- [`./docs/stargz.md`](./docs/stargz.md): Lazy-pulling using Stargz Snapshotter

cmd/nerdctl/run.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,11 @@ func setCreateFlags(cmd *cobra.Command) {
115115

116116
// #region network flags
117117
// network (net) is defined as StringSlice, not StringArray, to allow specifying "--network=cni1,cni2"
118-
cmd.Flags().StringSlice("network", []string{netutil.DefaultNetworkName}, `Connect a container to a network ("bridge"|"host"|"none")`)
118+
cmd.Flags().StringSlice("network", []string{netutil.DefaultNetworkName}, `Connect a container to a network ("bridge"|"host"|"none"|<CNI>)`)
119119
cmd.RegisterFlagCompletionFunc("network", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
120120
return shellCompleteNetworkNames(cmd, []string{})
121121
})
122-
cmd.Flags().StringSlice("net", []string{netutil.DefaultNetworkName}, `Connect a container to a network ("bridge"|"host"|"none")`)
122+
cmd.Flags().StringSlice("net", []string{netutil.DefaultNetworkName}, `Connect a container to a network ("bridge"|"host"|"none"|<CNI>)`)
123123
cmd.RegisterFlagCompletionFunc("net", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
124124
return shellCompleteNetworkNames(cmd, []string{})
125125
})

docs/cni.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Using CNI with nerdctl
2+
3+
nerdctl uses CNI plugins for its container network, you can set network by
4+
either `--network` or `--net` option.
5+
6+
## Basic networks
7+
8+
nerdctl support some basic types of CNI plugins without any configuration
9+
needed(you should have CNI plugin be installed), for Linux systems the basic
10+
CNI plugin types are `bridge`, `portmap`, `firewall`, `tuning`, for Windows
11+
system, the supported CNI plugin types are `nat` only.
12+
13+
The default network `bridge` for Linux and `nat` for Windows if you
14+
don't set any network options.
15+
16+
## Custom networks
17+
18+
You can also customize your CNI network by providing configuration files.
19+
For example you have one configuration file(`/etc/cni/net.d/10-mynet.conf`)
20+
for `bridge` network:
21+
22+
```json
23+
{
24+
"cniVersion": "0.4.0",
25+
"name": "mynet",
26+
"type": "bridge",
27+
"bridge": "cni0",
28+
"isGateway": true,
29+
"ipMasq": true,
30+
"ipam": {
31+
"type": "host-local",
32+
"subnet": "172.19.0.0/24",
33+
"routes": [
34+
{ "dst": "0.0.0.0/0" }
35+
]
36+
}
37+
}
38+
```
39+
40+
This will configure a new CNI network with the name `mynet`, and you can use
41+
this network to create a container:
42+
43+
```console
44+
# nerdctl run -it --net mynet --rm alpine ip addr show
45+
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
46+
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
47+
inet 127.0.0.1/8 scope host lo
48+
valid_lft forever preferred_lft forever
49+
inet6 ::1/128 scope host
50+
valid_lft forever preferred_lft forever
51+
3: eth0@if6120: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP
52+
link/ether 5e:5b:3f:0c:36:56 brd ff:ff:ff:ff:ff:ff
53+
inet 172.19.0.51/24 brd 172.19.0.255 scope global eth0
54+
valid_lft forever preferred_lft forever
55+
inet6 fe80::5c5b:3fff:fe0c:3656/64 scope link tentative
56+
valid_lft forever preferred_lft forever
57+
```
58+
59+
## Bridge Isolation Plugin
60+
61+
If you have the [CNI isolation plugin](https://github.com/AkihiroSuda/cni-isolation) installed, the `isolation` plugin will be used automatically.

pkg/netutil/netutil.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ type ConfigListTemplateOpts struct {
5555
ID int
5656
Name string // e.g. "nerdctl"
5757
Labels string // e.g. `{"version":"1.1.0"}`
58-
Subnet string // e.g. "10.4.0.0/16"
58+
Subnet string // e.g. "10.4.0.0/24"
5959
Gateway string // e.g. "10.4.0.1"
6060
ExtraPlugins string // e.g. `,{"type":"isolation"}`
6161
}

0 commit comments

Comments
 (0)