feat(network): let a service opt out of its name as a network alias

The service name is always registered as a DNS alias on every network a
service is attached to, so two projects sharing a network and running a
service with the same name resolve it to either container.

Honor the use_service_name_alias attribute of a service's network
entry: when false, the service name is not registered as an alias on
that network only. The container name alias, the user's aliases and the
service's other networks are untouched, and --use-aliases still decides
whether any alias is set. Unset keeps the current behavior.

The attribute comes from compose-go, through its ServiceNameAliasEnabled
helper. go.mod temporarily replaces compose-go with its main branch,
which has it: drop the replace once a version containing it is tagged.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Signed-off-by: Guillaume Lours <glours@users.noreply.github.com>
This commit is contained in:
Guillaume Lours 2026-10-14 18:40:09 +02:00 • committed by Nicolas De loof
parent 9e68c0e7ab
commit f647bc01d4
4 changed files with 53 additions and 4 deletions

2
go.mod
View file

@ -7,7 +7,7 @@ require (
github.com/Microsoft/go-winio v0.6.3
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d
github.com/buger/goterm v1.0.4
github.com/compose-spec/compose-go/v2 v2.16.2-0.20261012152039-c53b35077dd1
github.com/compose-spec/compose-go/v2 v2.16.2-0.20261015044941-5515644a24af
github.com/containerd/console v1.0.5
github.com/containerd/containerd/v2 v2.4.2
github.com/containerd/errdefs v1.0.0

4
go.sum
View file

@ -30,8 +30,8 @@ github.com/cloudflare/circl v1.6.3 h1:9GPOhQGF9MCYUeXyMYlqTR6a5gTrgR/fBLXvUgtVcg
github.com/cloudflare/circl v1.6.3/go.mod h1:2eXP6Qfat4O/Yhh8BznvKnJ+uzEoTQ6jVKJRn81BiS4=
github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb h1:EDmT6Q9Zs+SbUoc7Ik9EfrFqcylYqgPZ9ANSbTAntnE=
github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb/go.mod h1:ZjrT6AXHbDs86ZSdt/osfBi5qfexBrKUdONk989Wnk4=
github.com/compose-spec/compose-go/v2 v2.16.2-0.20261012152039-c53b35077dd1 h1:zlQ2D89Te4kZ0CPSywYwptC3h/fxjvz1rPSff/Eo2aM=
github.com/compose-spec/compose-go/v2 v2.16.2-0.20261012152039-c53b35077dd1/go.mod h1:Q1+qtN4vhzEjGrnqRtzx1xa8raDZQlMUe3WJxndYNiQ=
github.com/compose-spec/compose-go/v2 v2.16.2-0.20261015044941-5515644a24af h1:TOZLcS+qr4t2Xv8Wh0i9ebHLF7nlRt2hZ59pI6s49z8=
github.com/compose-spec/compose-go/v2 v2.16.2-0.20261015044941-5515644a24af/go.mod h1:Q1+qtN4vhzEjGrnqRtzx1xa8raDZQlMUe3WJxndYNiQ=
github.com/containerd/cgroups/v3 v3.1.3 h1:eUNflyMddm18+yrDmZPn3jI7C5hJ9ahABE5q6dyLYXQ=
github.com/containerd/cgroups/v3 v3.1.3/go.mod h1:PKZ2AcWmSBsY/tJUVhtS/rluX0b1uq1GmPO1ElCmbOw=
github.com/containerd/console v1.0.5 h1:R0ymNeydRqH2DmakFNdmjR2k0t7UPuiOV/N/27/qqsc=

View file

@ -514,7 +514,9 @@ func (s *composeService) prepareContainerMACAddress(service types.ServiceConfig,
func getAliases(project *types.Project, service types.ServiceConfig, serviceIndex int, cfg *types.ServiceNetworkConfig, useNetworkAliases bool) []string {
aliases := []string{getContainerName(project.Name, service, serviceIndex)}
if useNetworkAliases {
aliases = append(aliases, service.Name)
if cfg.ServiceNameAliasEnabled() {
aliases = append(aliases, service.Name)
}
if cfg != nil {
aliases = append(aliases, cfg.Aliases...)
}

View file

@ -360,6 +360,53 @@ func TestCreateEndpointSettings(t *testing.T) {
}, cmpopts.EquateComparable(netip.Addr{})))
}
func TestGetAliases(t *testing.T) {
enabled, disabled := true, false
tests := []struct {
name string
cfg *composetypes.ServiceNetworkConfig
useNetworkAliases bool
expected []string
}{
{
name: "no network config",
useNetworkAliases: true,
expected: []string{"containerName", "serviceName"},
},
{
name: "use_service_name_alias unset",
cfg: &composetypes.ServiceNetworkConfig{Aliases: []string{"alias1"}},
useNetworkAliases: true,
expected: []string{"containerName", "serviceName", "alias1"},
},
{
name: "use_service_name_alias true",
cfg: &composetypes.ServiceNetworkConfig{Aliases: []string{"alias1"}, UseServiceNameAlias: &enabled},
useNetworkAliases: true,
expected: []string{"containerName", "serviceName", "alias1"},
},
{
name: "use_service_name_alias false keeps the container name and the aliases",
cfg: &composetypes.ServiceNetworkConfig{Aliases: []string{"alias1"}, UseServiceNameAlias: &disabled},
useNetworkAliases: true,
expected: []string{"containerName", "alias1"},
},
{
name: "network aliases disabled for the run",
cfg: &composetypes.ServiceNetworkConfig{Aliases: []string{"alias1"}},
useNetworkAliases: false,
expected: []string{"containerName"},
},
}
service := composetypes.ServiceConfig{Name: "serviceName", ContainerName: "containerName"}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
aliases := getAliases(&composetypes.Project{Name: "projName"}, service, 0, tt.cfg, tt.useNetworkAliases)
assert.DeepEqual(t, aliases, tt.expected)
})
}
}
func Test_buildContainerVolumes(t *testing.T) {
pwd, err := os.Getwd()
assert.NilError(t, err)