check container_name is not in use by another service we will create

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2024-04-11 09:05:40 +02:00 коммит произвёл Nicolas De loof
Родитель ca734ce565
Коммит d239f0f318
6 изменённых файлов: 70 добавлений и 4 удалений

Просмотреть файл

@ -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()

2
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

4
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=

Просмотреть файл

@ -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
}

Просмотреть файл

@ -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")
}

Просмотреть файл

@ -0,0 +1,10 @@
services:
test:
image: alpine
container_name: test
command: /bin/true
another_test:
image: alpine
container_name: test
command: /bin/true