diff --git a/cli/command/stack/swarm/deploy_bundlefile.go b/cli/command/stack/swarm/deploy_bundlefile.go index b3819ef0f4..c730f3a231 100644 --- a/cli/command/stack/swarm/deploy_bundlefile.go +++ b/cli/command/stack/swarm/deploy_bundlefile.go @@ -39,7 +39,7 @@ func deployBundle(ctx context.Context, dockerCli command.Cli, opts options.Deplo networks := make(map[string]types.NetworkCreate) for _, service := range bundle.Services { for _, networkName := range service.Networks { - networks[networkName] = types.NetworkCreate{ + networks[namespace.Scope(networkName)] = types.NetworkCreate{ Labels: convert.AddStackLabel(namespace, nil), } } diff --git a/cli/command/stack/swarm/deploy_composefile.go b/cli/command/stack/swarm/deploy_composefile.go index 0a3f2ac720..c9d5a3c158 100644 --- a/cli/command/stack/swarm/deploy_composefile.go +++ b/cli/command/stack/swarm/deploy_composefile.go @@ -181,8 +181,7 @@ func createNetworks( existingNetworkMap[network.Name] = network } - for internalName, createOpts := range networks { - name := namespace.Scope(internalName) + for name, createOpts := range networks { if _, exists := existingNetworkMap[name]; exists { continue } @@ -193,7 +192,7 @@ func createNetworks( fmt.Fprintf(dockerCli.Out(), "Creating network %s\n", name) if _, err := client.NetworkCreate(ctx, name, createOpts); err != nil { - return errors.Wrapf(err, "failed to create network %s", internalName) + return errors.Wrapf(err, "failed to create network %s", name) } } return nil diff --git a/cli/compose/convert/compose.go b/cli/compose/convert/compose.go index 03f4524d6b..7b369596f9 100644 --- a/cli/compose/convert/compose.go +++ b/cli/compose/convert/compose.go @@ -87,7 +87,12 @@ func Networks(namespace Namespace, networks networkMap, servicesNetworks map[str } createOpts.IPAM.Config = append(createOpts.IPAM.Config, config) } - result[internalName] = createOpts + + networkName := namespace.Scope(internalName) + if network.Name != "" { + networkName = network.Name + } + result[networkName] = createOpts } return result, externalNetworks diff --git a/cli/compose/convert/compose_test.go b/cli/compose/convert/compose_test.go index a2ff2f1723..1846df17ee 100644 --- a/cli/compose/convert/compose_test.go +++ b/cli/compose/convert/compose_test.go @@ -35,6 +35,7 @@ func TestNetworks(t *testing.T) { "outside": {}, "default": {}, "attachablenet": {}, + "named": {}, } source := networkMap{ "normal": composetypes.NetworkConfig{ @@ -62,14 +63,17 @@ func TestNetworks(t *testing.T) { Driver: "overlay", Attachable: true, }, + "named": composetypes.NetworkConfig{ + Name: "othername", + }, } expected := map[string]types.NetworkCreate{ - "default": { + "foo_default": { Labels: map[string]string{ LabelNamespace: "foo", }, }, - "normal": { + "foo_normal": { Driver: "overlay", IPAM: &network.IPAM{ Driver: "driver", @@ -87,18 +91,21 @@ func TestNetworks(t *testing.T) { "something": "labeled", }, }, - "attachablenet": { + "foo_attachablenet": { Driver: "overlay", Attachable: true, Labels: map[string]string{ LabelNamespace: "foo", }, }, + "othername": { + Labels: map[string]string{LabelNamespace: "foo"}, + }, } networks, externals := Networks(namespace, source, serviceNetworks) - assert.Check(t, is.DeepEqual(expected, networks)) - assert.Check(t, is.DeepEqual([]string{"special"}, externals)) + assert.DeepEqual(t, expected, networks) + assert.DeepEqual(t, []string{"special"}, externals) } func TestSecrets(t *testing.T) { diff --git a/cli/compose/convert/service.go b/cli/compose/convert/service.go index cddedd8c0c..9a20db1532 100644 --- a/cli/compose/convert/service.go +++ b/cli/compose/convert/service.go @@ -229,7 +229,7 @@ func convertServiceNetworks( aliases = network.Aliases } target := namespace.Scope(networkName) - if networkConfig.External.External { + if networkConfig.Name != "" { target = networkConfig.Name } netAttachConfig := swarm.NetworkAttachmentConfig{ diff --git a/cli/compose/loader/loader_test.go b/cli/compose/loader/loader_test.go index 4219593725..64b7406e60 100644 --- a/cli/compose/loader/loader_test.go +++ b/cli/compose/loader/loader_test.go @@ -10,6 +10,7 @@ import ( "time" "github.com/docker/cli/cli/compose/types" + "github.com/google/go-cmp/cmp/cmpopts" "github.com/gotestyourself/gotestyourself/assert" is "github.com/gotestyourself/gotestyourself/assert/cmp" "github.com/sirupsen/logrus" @@ -1357,3 +1358,40 @@ networks: assert.ErrorContains(t, err, "network.external.name and network.name conflict; only use network.name") assert.ErrorContains(t, err, "foo") } + +func TestLoadNetworkWithName(t *testing.T) { + config, err := loadYAML(` +version: '3.5' +services: + hello-world: + image: redis:alpine + networks: + - network1 + - network3 + +networks: + network1: + name: network2 + network3: +`) + assert.NilError(t, err) + expected := &types.Config{ + Filename: "filename.yml", + Version: "3.5", + Services: types.Services{ + { + Name: "hello-world", + Image: "redis:alpine", + Networks: map[string]*types.ServiceNetworkConfig{ + "network1": nil, + "network3": nil, + }, + }, + }, + Networks: map[string]types.NetworkConfig{ + "network1": {Name: "network2"}, + "network3": {}, + }, + } + assert.DeepEqual(t, config, expected, cmpopts.EquateEmpty()) +}