Skip to content

Commit 03d2f9f

Browse files
Merge pull request moby#4978 from profnandaa/docs-windows-cni-setup
docs: add details for setting up CNI for windows
2 parents e740d4b + 97b80f2 commit 03d2f9f

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

docs/windows.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- [Setup Instructions](#setup-instructions)
99
- [Example Build](#example-build)
1010
- [Running `buildctl` from a Non-Admin Terminal](#running-buildctl-from-a-non-admin-terminal)
11+
- [CNI / Networking Setup](#cni--networking-setup)
1112

1213
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
1314

@@ -40,6 +41,7 @@ Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V, Containers
4041
You will be asked to restart your machine, do so, and then continue with the rest of the steps. No other restart needed.
4142

4243
1. Setup `containerd` by following [the setup instructions here](https://github.com/containerd/containerd/blob/main/docs/getting-started.md#installing-containerd-on-windows). (_Currently, we only support the `containerd` worker_.)
44+
1. Setup the CNI, see details in the [CNI / Networking Setup](#cni--networking-setup) section.
4345
1. Start the `containerd` service, if not yet started.
4446
1. Download and extract:
4547
```powershell
@@ -76,6 +78,50 @@ You will be asked to restart your machine, do so, and then continue with the res
7678
time="2024-02-26T10:42:16+03:00" level=warning msg="currently, only the default worker can be used."
7779
time="2024-02-26T10:42:16+03:00" level=info msg="running server on //./pipe/buildkitd"
7880
```
81+
82+
**Running `buildkitd` with the CNI:**
83+
84+
Note that the above simple run will not have the networking bit setup;
85+
for instance you won't be able to access the internet from the builds e.g. downloading resources.
86+
87+
Follow the instructions in the [CNI / Networking Setup](#cni--networking-setup) section.
88+
Once that is done, you can now start `buildkitd` providing the binary and config paths to the flags.
89+
These are the same paths used by `containerd` too:
90+
91+
```powershell
92+
buildkitd `
93+
--containerd-cni-config-path="C:\Program Files\containerd\cni\conf\0-containerd-nat.conf" `
94+
--containerd-cni-binary-dir="C:\Program Files\containerd\cni\bin"
95+
```
96+
97+
You can also run `buildkitd` as a _Windows Service_:
98+
99+
```powershell
100+
buildkitd `
101+
--register-service `
102+
--service-name buildkitd `
103+
--containerd-cni-config-path="C:\Program Files\containerd\cni\conf\0-containerd-nat.conf" `
104+
--containerd-cni-binary-dir="C:\Program Files\containerd\cni\bin" `
105+
--debug `
106+
--log-file="C:\Windows\Temp\buildkitd.log
107+
```
108+
109+
> **NOTE:** the above `log-file` path is just an example, but make sure to set up
110+
> your logs properly.
111+
112+
`buildkitd` on Windows depends on `containerd`. You can make the above registered `buildkitd` service
113+
dependent on `containerd` (the naming may vary). The space after `=` is required:
114+
115+
```powershell
116+
sc.exe config buildkitd depend= containerd
117+
```
118+
119+
We can also set the service to start automatically:
120+
121+
```powershell
122+
Set-Service -StartupType Automatic buildkitd
123+
```
124+
79125
1. In another terminal (still elevated), try out a `buildctl` command to test that the setup is good:
80126
```
81127
PS> buildctl debug info
@@ -181,3 +227,54 @@ buildkitd --group="USERBOX-1\buildkit-users"
181227
```
182228

183229
With this now, you can run `buildctl` in a non-admin terminal.
230+
231+
## CNI / Networking Setup
232+
233+
Below is a simple setup based on the `nat` network that comes by default, with enabling the
234+
_containers_ and _Hyper-V_ features.
235+
236+
```powershell
237+
$networkName = 'nat'
238+
# Get-HnsNetwork is available once you have enabled the 'Hyper-V Host Compute Service' feature
239+
# which must have been done at the Quick setup above
240+
# Enable-WindowsOptionalFeature -Online -FeatureName containers -All
241+
# Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V-All -All
242+
# the default one named `nat` should be available
243+
$natInfo = Get-HnsNetwork -ErrorAction Ignore | Where-Object { $_.Name -eq $networkName }
244+
if ($null -eq $natInfo) {
245+
throw "NAT network not found, check if you enabled containers, Hyper-V features and restarted the machine"
246+
}
247+
$gateway = $natInfo.Subnets[0].GatewayAddress
248+
$subnet = $natInfo.Subnets[0].AddressPrefix
249+
250+
$cniConfPath = "$env:ProgramFiles\containerd\cni\conf\0-containerd-nat.conf"
251+
$cniBinDir = "$env:ProgramFiles\containerd\cni\bin"
252+
$cniVersion = "0.3.0"
253+
254+
# get the CNI plugins (binaries)
255+
mkdir $cniBinDir -Force
256+
curl.exe -LO https://github.com/microsoft/windows-container-networking/releases/download/v$cniVersion/windows-container-networking-cni-amd64-v$cniVersion.zip
257+
tar xvf windows-container-networking-cni-amd64-v$cniVersion.zip -C $cniBinDir
258+
259+
$natConfig = @"
260+
{
261+
"cniVersion": "$cniVersion",
262+
"name": "$networkName",
263+
"type": "nat",
264+
"master": "Ethernet",
265+
"ipam": {
266+
"subnet": "$subnet",
267+
"routes": [
268+
{
269+
"gateway": "$gateway"
270+
}
271+
]
272+
},
273+
"capabilities": {
274+
"portMappings": true,
275+
"dns": true
276+
}
277+
}
278+
"@
279+
Set-Content -Path $cniConfPath -Value $natConfig
280+
```

0 commit comments

Comments
 (0)