Skip to content

Commit 9cc48be

Browse files
committed
daemon: only add short cid to aliases for custom networks
Prior to 7a9b680, the container short ID was added to the network aliases only for custom networks. However, this logic wasn't preserved in 6a2542d and now the cid is always added to the list of network aliases. This commit reintroduces the old logic. Signed-off-by: Albin Kerouanton <[email protected]>
1 parent 12c7411 commit 9cc48be

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

daemon/inspect.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/docker/docker/api/types"
1010
"github.com/docker/docker/api/types/backend"
11+
containertypes "github.com/docker/docker/api/types/container"
1112
networktypes "github.com/docker/docker/api/types/network"
1213
"github.com/docker/docker/api/types/versions"
1314
"github.com/docker/docker/api/types/versions/v1p20"
@@ -36,8 +37,10 @@ func (daemon *Daemon) ContainerInspect(ctx context.Context, name string, size bo
3637
}
3738

3839
shortCID := stringid.TruncateID(ctr.ID)
39-
for _, ep := range ctr.NetworkSettings.Networks {
40-
ep.Aliases = sliceutil.Dedup(append(ep.Aliases, shortCID, ctr.Config.Hostname))
40+
for nwName, ep := range ctr.NetworkSettings.Networks {
41+
if containertypes.NetworkMode(nwName).IsUserDefined() {
42+
ep.Aliases = sliceutil.Dedup(append(ep.Aliases, shortCID, ctr.Config.Hostname))
43+
}
4144
}
4245

4346
return ctr, nil

integration/container/inspect_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77
"time"
88

9+
containertypes "github.com/docker/docker/api/types/container"
910
"github.com/docker/docker/client"
1011
"github.com/docker/docker/integration/internal/container"
1112
"github.com/docker/docker/testutil/request"
@@ -68,3 +69,28 @@ func TestInspectAnnotations(t *testing.T) {
6869
assert.NilError(t, err)
6970
assert.Check(t, is.DeepEqual(inspect.HostConfig.Annotations, annotations))
7071
}
72+
73+
func TestNetworkAliasesAreEmpty(t *testing.T) {
74+
skip.If(t, testEnv.DaemonInfo.OSType == "windows")
75+
76+
ctx := setupTest(t)
77+
apiClient := request.NewAPIClient(t)
78+
79+
for _, nwMode := range []string{"host", "bridge"} {
80+
t.Run(nwMode, func(t *testing.T) {
81+
ctr := container.Run(ctx, t, apiClient,
82+
container.WithName("ctr-"+nwMode),
83+
container.WithImage("busybox:latest"),
84+
container.WithCmd("top"),
85+
container.WithNetworkMode(nwMode))
86+
defer apiClient.ContainerRemove(ctx, ctr, containertypes.RemoveOptions{
87+
Force: true,
88+
})
89+
90+
inspect := container.Inspect(ctx, t, apiClient, ctr)
91+
netAliases := inspect.NetworkSettings.Networks[nwMode].Aliases
92+
93+
assert.Check(t, is.Nil(netAliases))
94+
})
95+
}
96+
}

0 commit comments

Comments
 (0)