Skip to content

Commit be22bc7

Browse files
milasndeloof
authored andcommitted
network: fix random missing network when service has more than one
As part of the fix for docker#10668, the logic was adjusted so that the default (highest-priority) network is used in the `ContainerCreate`, and then the remaining networks are connected via calls to `NetworkConnect` before starting the container. Unfortunately, `ServiceConfig::NetworksByPriority` is neither deterministic nor stable when networks have the same priority. It's non-deterministic because the order of networks from parsing YAML is random, since they are loaded into a Go map (which have random iteration order). Additionally, it's not using a `SortStable` in `compose-go`, so even if the load order was predictable, it still might produce different results. While I look at improving `compose-go` here to prevent this from tripping us up in the future, this fix looks at _all_ networks for a service and ignores the "default" one now. Before, it would always skip the first one in the slice since that _should_ have been the "default". Signed-off-by: Milas Bowman <[email protected]>
1 parent b5f5e27 commit be22bc7

File tree

2 files changed

+29
-40
lines changed

2 files changed

+29
-40
lines changed

pkg/compose/convergence.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -549,13 +549,15 @@ func (s *composeService) createMobyContainer(ctx context.Context,
549549
// call via container.NetworkMode & network.NetworkingConfig
550550
// any remaining networks are connected one-by-one here after creation (but before start)
551551
serviceNetworks := service.NetworksByPriority()
552-
if len(serviceNetworks) > 1 {
553-
for _, networkKey := range serviceNetworks[1:] {
554-
mobyNetworkName := project.Networks[networkKey].Name
555-
epSettings := createEndpointSettings(project, service, number, networkKey, cfgs.Links, opts.UseNetworkAliases)
556-
if err := s.apiClient().NetworkConnect(ctx, mobyNetworkName, created.ID, epSettings); err != nil {
557-
return created, err
558-
}
552+
for _, networkKey := range serviceNetworks {
553+
mobyNetworkName := project.Networks[networkKey].Name
554+
if string(cfgs.Host.NetworkMode) == mobyNetworkName {
555+
// primary network already configured as part of ContainerCreate
556+
continue
557+
}
558+
epSettings := createEndpointSettings(project, service, number, networkKey, cfgs.Links, opts.UseNetworkAliases)
559+
if err := s.apiClient().NetworkConnect(ctx, mobyNetworkName, created.ID, epSettings); err != nil {
560+
return created, err
559561
}
560562
}
561563

pkg/e2e/networks_test.go

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,47 +29,34 @@ import (
2929

3030
func TestNetworks(t *testing.T) {
3131
// fixture is shared with TestNetworkModes and is not safe to run concurrently
32-
c := NewCLI(t)
33-
3432
const projectName = "network-e2e"
33+
c := NewCLI(t, WithEnv(
34+
"COMPOSE_PROJECT_NAME="+projectName,
35+
"COMPOSE_FILE=./fixtures/network-test/compose.yaml",
36+
))
3537

36-
t.Run("ensure we do not reuse previous networks", func(t *testing.T) {
37-
c.RunDockerOrExitError(t, "network", "rm", projectName+"-dbnet")
38-
c.RunDockerOrExitError(t, "network", "rm", "microservices")
39-
})
40-
41-
t.Run("up", func(t *testing.T) {
42-
c.RunDockerComposeCmd(t, "-f", "./fixtures/network-test/compose.yaml", "--project-name", projectName, "up",
43-
"-d")
44-
})
38+
c.RunDockerComposeCmd(t, "down", "-t0", "-v")
4539

46-
t.Run("check running project", func(t *testing.T) {
47-
res := c.RunDockerComposeCmd(t, "-p", projectName, "ps")
48-
res.Assert(t, icmd.Expected{Out: `web`})
40+
c.RunDockerComposeCmd(t, "up", "-d")
4941

50-
endpoint := "http://localhost:80"
51-
output := HTTPGetWithRetry(t, endpoint+"/words/noun", http.StatusOK, 2*time.Second, 20*time.Second)
52-
assert.Assert(t, strings.Contains(output, `"word":`))
42+
res := c.RunDockerComposeCmd(t, "ps")
43+
res.Assert(t, icmd.Expected{Out: `web`})
5344

54-
res = c.RunDockerCmd(t, "network", "ls")
55-
res.Assert(t, icmd.Expected{Out: projectName + "_dbnet"})
56-
res.Assert(t, icmd.Expected{Out: "microservices"})
57-
})
45+
endpoint := "http://localhost:80"
46+
output := HTTPGetWithRetry(t, endpoint+"/words/noun", http.StatusOK, 2*time.Second, 20*time.Second)
47+
assert.Assert(t, strings.Contains(output, `"word":`))
5848

59-
t.Run("port", func(t *testing.T) {
60-
res := c.RunDockerComposeCmd(t, "--project-name", projectName, "port", "words", "8080")
61-
res.Assert(t, icmd.Expected{Out: `0.0.0.0:8080`})
62-
})
49+
res = c.RunDockerCmd(t, "network", "ls")
50+
res.Assert(t, icmd.Expected{Out: projectName + "_dbnet"})
51+
res.Assert(t, icmd.Expected{Out: "microservices"})
6352

64-
t.Run("down", func(t *testing.T) {
65-
_ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
66-
})
53+
res = c.RunDockerComposeCmd(t, "port", "words", "8080")
54+
res.Assert(t, icmd.Expected{Out: `0.0.0.0:8080`})
6755

68-
t.Run("check networks after down", func(t *testing.T) {
69-
res := c.RunDockerCmd(t, "network", "ls")
70-
assert.Assert(t, !strings.Contains(res.Combined(), projectName), res.Combined())
71-
assert.Assert(t, !strings.Contains(res.Combined(), "microservices"), res.Combined())
72-
})
56+
c.RunDockerComposeCmd(t, "down", "-t0", "-v")
57+
res = c.RunDockerCmd(t, "network", "ls")
58+
assert.Assert(t, !strings.Contains(res.Combined(), projectName), res.Combined())
59+
assert.Assert(t, !strings.Contains(res.Combined(), "microservices"), res.Combined())
7360
}
7461

7562
func TestNetworkAliases(t *testing.T) {

0 commit comments

Comments
 (0)