From d239f0f3187a2ed5404c61f83bd5e995c81600ff Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Thu, 11 Apr 2024 09:05:40 +0200 Subject: [PATCH] check container_name is not in use by another service we will create Signed-off-by: Nicolas De Loof --- cmd/compose/config.go | 7 ++++ go.mod | 2 +- go.sum | 4 +- pkg/compose/create.go | 7 +++- pkg/e2e/container_name_test.go | 44 ++++++++++++++++++++ pkg/e2e/fixtures/container_name/compose.yaml | 10 +++++ 6 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 pkg/e2e/container_name_test.go create mode 100644 pkg/e2e/fixtures/container_name/compose.yaml diff --git a/cmd/compose/config.go b/cmd/compose/config.go index 4851e773cb..ab6d5c583a 100644 --- a/cmd/compose/config.go +++ b/cmd/compose/config.go @@ -169,6 +169,13 @@ func runConfig(ctx context.Context, dockerCli command.Cli, opts configOptions, s return err } + if !opts.noConsistency { + err := project.CheckContainerNameUnicity() + if err != nil { + return err + } + } + switch opts.Format { case "json": content, err = project.MarshalJSON() diff --git a/go.mod b/go.mod index 27a362d645..5ed9d3bd90 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/Microsoft/go-winio v0.6.1 github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d github.com/buger/goterm v1.0.4 - github.com/compose-spec/compose-go/v2 v2.0.3-0.20240407191136-f388192b8a39 + github.com/compose-spec/compose-go/v2 v2.0.3-0.20240416141209-60aa6409b2c4 github.com/containerd/console v1.0.4 github.com/containerd/containerd v1.7.13 github.com/davecgh/go-spew v1.1.1 diff --git a/go.sum b/go.sum index 7a97c621f7..a9c72e1d4d 100644 --- a/go.sum +++ b/go.sum @@ -90,8 +90,8 @@ github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4 h1:/inchEIKaYC1Akx+H+g github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= 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.0.3-0.20240407191136-f388192b8a39 h1:ZUpnv0xA75X9gy9Y7hjJm51nflGbr+2URaLXBtEic7A= -github.com/compose-spec/compose-go/v2 v2.0.3-0.20240407191136-f388192b8a39/go.mod h1:bEPizBkIojlQ20pi2vNluBa58tevvj0Y18oUSHPyfdc= +github.com/compose-spec/compose-go/v2 v2.0.3-0.20240416141209-60aa6409b2c4 h1:WYiZ9D0WBykHUJLlpt+w7NXX0hy+cQKKdVe7vmsNZvg= +github.com/compose-spec/compose-go/v2 v2.0.3-0.20240416141209-60aa6409b2c4/go.mod h1:bEPizBkIojlQ20pi2vNluBa58tevvj0Y18oUSHPyfdc= github.com/containerd/cgroups v1.1.0 h1:v8rEWFl6EoqHB+swVNjVoCJE8o3jX7e8nqBGPLaDFBM= github.com/containerd/cgroups v1.1.0/go.mod h1:6ppBcbh/NOOUU+dMKrykgaBnK9lCIBxHqJDGwsa1mIw= github.com/containerd/console v1.0.4 h1:F2g4+oChYvBTsASRTz8NP6iIAi97J3TtSAsLbIFn4ro= diff --git a/pkg/compose/create.go b/pkg/compose/create.go index ad78c329f9..b815cb66b3 100644 --- a/pkg/compose/create.go +++ b/pkg/compose/create.go @@ -77,8 +77,13 @@ func (s *composeService) create(ctx context.Context, project *types.Project, opt options.Services = project.ServiceNames() } + err := project.CheckContainerNameUnicity() + if err != nil { + return err + } + var observedState Containers - observedState, err := s.getContainers(ctx, project.Name, oneOffInclude, true) + observedState, err = s.getContainers(ctx, project.Name, oneOffInclude, true) if err != nil { return err } diff --git a/pkg/e2e/container_name_test.go b/pkg/e2e/container_name_test.go new file mode 100644 index 0000000000..1ddc489a19 --- /dev/null +++ b/pkg/e2e/container_name_test.go @@ -0,0 +1,44 @@ +//go:build !windows +// +build !windows + +/* + Copyright 2022 Docker Compose CLI authors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package e2e + +import ( + "testing" + + "gotest.tools/v3/icmd" +) + +func TestUpContainerNameConflict(t *testing.T) { + c := NewParallelCLI(t) + const projectName = "e2e-container_name_conflict" + + t.Cleanup(func() { + c.RunDockerComposeCmd(t, "--project-name", projectName, "down") + }) + + res := c.RunDockerComposeCmdNoCheck(t, "-f", "fixtures/container_name/compose.yaml", "--project-name", projectName, "up") + res.Assert(t, icmd.Expected{ExitCode: 1, Err: `container name "test" is already in use`}) + + c.RunDockerComposeCmd(t, "--project-name", projectName, "down") + c.RunDockerComposeCmd(t, "-f", "fixtures/container_name/compose.yaml", "--project-name", projectName, "up", "test") + + c.RunDockerComposeCmd(t, "--project-name", projectName, "down") + c.RunDockerComposeCmd(t, "-f", "fixtures/container_name/compose.yaml", "--project-name", projectName, "up", "another_test") +} diff --git a/pkg/e2e/fixtures/container_name/compose.yaml b/pkg/e2e/fixtures/container_name/compose.yaml new file mode 100644 index 0000000000..c967b885d8 --- /dev/null +++ b/pkg/e2e/fixtures/container_name/compose.yaml @@ -0,0 +1,10 @@ +services: + test: + image: alpine + container_name: test + command: /bin/true + + another_test: + image: alpine + container_name: test + command: /bin/true