浏览代码

Merge pull request #47181 from akerouanton/fix-aliases-on-default-bridge

daemon: only add short cid to aliases for custom networks
Sebastiaan van Stijn 1 年之前
父节点
当前提交
9763709c05
共有 2 个文件被更改,包括 37 次插入2 次删除
  1. 5 2
      daemon/inspect.go
  2. 32 0
      integration/container/inspect_test.go

+ 5 - 2
daemon/inspect.go

@@ -8,6 +8,7 @@ import (
 
 
 	"github.com/docker/docker/api/types"
 	"github.com/docker/docker/api/types"
 	"github.com/docker/docker/api/types/backend"
 	"github.com/docker/docker/api/types/backend"
+	containertypes "github.com/docker/docker/api/types/container"
 	networktypes "github.com/docker/docker/api/types/network"
 	networktypes "github.com/docker/docker/api/types/network"
 	"github.com/docker/docker/api/types/versions"
 	"github.com/docker/docker/api/types/versions"
 	"github.com/docker/docker/api/types/versions/v1p20"
 	"github.com/docker/docker/api/types/versions/v1p20"
@@ -36,8 +37,10 @@ func (daemon *Daemon) ContainerInspect(ctx context.Context, name string, size bo
 		}
 		}
 
 
 		shortCID := stringid.TruncateID(ctr.ID)
 		shortCID := stringid.TruncateID(ctr.ID)
-		for _, ep := range ctr.NetworkSettings.Networks {
-			ep.Aliases = sliceutil.Dedup(append(ep.Aliases, shortCID, ctr.Config.Hostname))
+		for nwName, ep := range ctr.NetworkSettings.Networks {
+			if containertypes.NetworkMode(nwName).IsUserDefined() {
+				ep.Aliases = sliceutil.Dedup(append(ep.Aliases, shortCID, ctr.Config.Hostname))
+			}
 		}
 		}
 
 
 		return ctr, nil
 		return ctr, nil

+ 32 - 0
integration/container/inspect_test.go

@@ -2,10 +2,12 @@ package container // import "github.com/docker/docker/integration/container"
 
 
 import (
 import (
 	"encoding/json"
 	"encoding/json"
+	"runtime"
 	"strings"
 	"strings"
 	"testing"
 	"testing"
 	"time"
 	"time"
 
 
+	containertypes "github.com/docker/docker/api/types/container"
 	"github.com/docker/docker/client"
 	"github.com/docker/docker/client"
 	"github.com/docker/docker/integration/internal/container"
 	"github.com/docker/docker/integration/internal/container"
 	"github.com/docker/docker/testutil/request"
 	"github.com/docker/docker/testutil/request"
@@ -68,3 +70,33 @@ func TestInspectAnnotations(t *testing.T) {
 	assert.NilError(t, err)
 	assert.NilError(t, err)
 	assert.Check(t, is.DeepEqual(inspect.HostConfig.Annotations, annotations))
 	assert.Check(t, is.DeepEqual(inspect.HostConfig.Annotations, annotations))
 }
 }
+
+// TestNetworkAliasesAreEmpty verifies that network-scoped aliases are not set
+// for non-custom networks (network-scoped aliases are only supported for
+// custom networks, except for the "Default Switch" network on Windows).
+func TestNetworkAliasesAreEmpty(t *testing.T) {
+	ctx := setupTest(t)
+	apiClient := request.NewAPIClient(t)
+
+	netModes := []string{"host", "bridge", "none"}
+	if runtime.GOOS == "windows" {
+		netModes = []string{"nat", "none"}
+	}
+
+	for _, nwMode := range netModes {
+		t.Run(nwMode, func(t *testing.T) {
+			ctr := container.Create(ctx, t, apiClient,
+				container.WithName("ctr-"+nwMode),
+				container.WithImage("busybox:latest"),
+				container.WithNetworkMode(nwMode))
+			defer apiClient.ContainerRemove(ctx, ctr, containertypes.RemoveOptions{
+				Force: true,
+			})
+
+			inspect := container.Inspect(ctx, t, apiClient, ctr)
+			netAliases := inspect.NetworkSettings.Networks[nwMode].Aliases
+
+			assert.Check(t, is.Nil(netAliases))
+		})
+	}
+}