Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions cmd/nerdctl/compose/compose_up_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1423,3 +1423,44 @@ services:

testCase.Run(t)
}

func TestComposeUpNetworkModeHostWithoutCNIPlugins(t *testing.T) {
testCase := nerdtest.Setup()

// --cni-path and --cni-netconfpath are nerdctl specific.
testCase.Require = require.Not(nerdtest.Docker)

testCase.Setup = func(data test.Data, helpers test.Helpers) {
dockerComposeYAML := fmt.Sprintf(`
services:
svc0:
image: %s
network_mode: host
command: "sleep infinity"
`, testutil.CommonImage)

data.Labels().Set("composeYAML", data.Temp().Save(dockerComposeYAML, "compose.yaml"))
// An empty CNI_PATH and an empty netconf dir together mimic a host that never
// installed the CNI plugins. Services using host networking do not need them.
data.Labels().Set("cniPath", data.Temp().Dir("cni-bin"))
data.Labels().Set("cniNetConfPath", data.Temp().Dir("cni-netconf"))
}

testCase.Command = func(data test.Data, helpers test.Helpers) test.TestableCommand {
return helpers.Command(
"--cni-path", data.Labels().Get("cniPath"),
"--cni-netconfpath", data.Labels().Get("cniNetConfPath"),
"compose", "-f", data.Labels().Get("composeYAML"), "up", "-d")
}

testCase.Expected = test.Expects(expect.ExitCodeSuccess, nil, nil)

testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
helpers.Anyhow(
"--cni-path", data.Labels().Get("cniPath"),
"--cni-netconfpath", data.Labels().Get("cniNetConfPath"),
"compose", "-f", data.Labels().Get("composeYAML"), "down", "-v")
}

testCase.Run(t)
}
6 changes: 5 additions & 1 deletion pkg/cmd/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ func New(client *containerd.Client, globalOptions types.GlobalCommandOptions, op
return nil, err
}

cniEnv, err := netutil.NewCNIEnv(globalOptions.CNIPath, globalOptions.CNINetConfPath, netutil.WithNamespace(globalOptions.Namespace), netutil.WithDefaultNetwork(globalOptions.BridgeIP))
// The default network is deliberately not created here. It is only needed by
// services that actually attach to it, and `nerdctl run` already creates it on
// demand. Creating it eagerly made every compose command require a CNI plugin,
// even for projects whose services all use network_mode: host or none.
cniEnv, err := netutil.NewCNIEnv(globalOptions.CNIPath, globalOptions.CNINetConfPath, netutil.WithNamespace(globalOptions.Namespace))
if err != nil {
return nil, err
}
Expand Down
Loading