Skip to content

Commit 8e278e2

Browse files
authored
Merge pull request #1232 from Zheaoli/manjusaka/fix-1149
Remove duplicate host record when update the DNS
2 parents 8203917 + df376b8 commit 8e278e2

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed

cmd/nerdctl/compose_up_linux_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,3 +437,56 @@ services:
437437
base.ComposeCmd("-f", comp.YAMLFullPath(), "down").AssertOK()
438438

439439
}
440+
441+
func TestComposeUpWithExternalNetwork(t *testing.T) {
442+
containerName1 := testutil.Identifier(t) + "-1"
443+
containerName2 := testutil.Identifier(t) + "-2"
444+
networkName := testutil.Identifier(t) + "-network"
445+
var dockerComposeYaml1 = fmt.Sprintf(`
446+
version: "3"
447+
services:
448+
%s:
449+
image: %s
450+
container_name: %s
451+
networks:
452+
%s:
453+
aliases:
454+
- nginx-1
455+
networks:
456+
%s:
457+
external: true
458+
`, containerName1, testutil.NginxAlpineImage, containerName1, networkName, networkName)
459+
var dockerComposeYaml2 = fmt.Sprintf(`
460+
version: "3"
461+
services:
462+
%s:
463+
image: %s
464+
container_name: %s
465+
networks:
466+
%s:
467+
aliases:
468+
- nginx-2
469+
networks:
470+
%s:
471+
external: true
472+
`, containerName2, testutil.NginxAlpineImage, containerName2, networkName, networkName)
473+
comp1 := testutil.NewComposeDir(t, dockerComposeYaml1)
474+
defer comp1.CleanUp()
475+
comp2 := testutil.NewComposeDir(t, dockerComposeYaml2)
476+
defer comp2.CleanUp()
477+
base := testutil.NewBase(t)
478+
// Create the test network
479+
base.Cmd("network", "create", networkName).AssertOK()
480+
defer base.Cmd("network", "rm", networkName).Run()
481+
// Run the first compose
482+
base.ComposeCmd("-f", comp1.YAMLFullPath(), "up", "-d").AssertOK()
483+
defer base.ComposeCmd("-f", comp1.YAMLFullPath(), "down", "-v").Run()
484+
// Run the second compose
485+
base.ComposeCmd("-f", comp2.YAMLFullPath(), "up", "-d").AssertOK()
486+
defer base.ComposeCmd("-f", comp2.YAMLFullPath(), "down", "-v").Run()
487+
// Down the second compose
488+
base.ComposeCmd("-f", comp2.YAMLFullPath(), "down", "-v").AssertOK()
489+
// Run the second compose again
490+
base.ComposeCmd("-f", comp2.YAMLFullPath(), "up", "-d").AssertOK()
491+
base.Cmd("exec", containerName1, "wget", "-qO-", "http://"+containerName2).AssertOutContains(testutil.NginxAlpineIndexHTMLSnippet)
492+
}

pkg/dnsutil/hostsstore/updater.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func (u *updater) phase2() error {
132132
var buf bytes.Buffer
133133
buf.WriteString(fmt.Sprintf("# %s\n", markerBegin))
134134
buf.WriteString("127.0.0.1 localhost localhost.localdomain\n")
135-
buf.WriteString(":1 localhost localhost.localdomain\n")
135+
buf.WriteString("::1 localhost localhost.localdomain\n")
136136

137137
// keep extra hosts first
138138
if u.id == myMeta.ID {
@@ -146,17 +146,26 @@ func (u *updater) phase2() error {
146146
if err != nil {
147147
return err
148148
}
149+
serviceHosts := make(map[string]string)
150+
for ip, host := range hosts {
151+
if ip == "127.0.0.1" || ip == "::1" {
152+
continue
153+
}
154+
serviceHosts[strings.Join(host, " ")] = ip
155+
}
149156

150157
// TODO: cut off entries for the containers in other networks
151158
for ip, nwName := range u.nwNameByIPStr {
152159
meta := u.metaByIPStr[ip]
153160
if line := createLine(nwName, meta, myNetworks); len(line) != 0 {
154-
hosts[ip] = line
161+
serviceHosts[strings.Join(line, " ")] = ip
155162
}
156163
}
157-
for ip, host := range hosts {
158-
buf.WriteString(fmt.Sprintf("%-15s %s\n", ip, strings.Join(host, " ")))
164+
165+
for line, ip := range serviceHosts {
166+
buf.WriteString(fmt.Sprintf("%-15s %s\n", ip, line))
159167
}
168+
160169
buf.WriteString(fmt.Sprintf("# %s\n", markerEnd))
161170
err = os.WriteFile(path, buf.Bytes(), 0644)
162171
if err != nil {

0 commit comments

Comments
 (0)