Skip to content

Commit d239f0f

Browse files
committed
check container_name is not in use by another service we will create
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent ca734ce commit d239f0f

File tree

6 files changed

+70
-4
lines changed

6 files changed

+70
-4
lines changed

cmd/compose/config.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,13 @@ func runConfig(ctx context.Context, dockerCli command.Cli, opts configOptions, s
169169
return err
170170
}
171171

172+
if !opts.noConsistency {
173+
err := project.CheckContainerNameUnicity()
174+
if err != nil {
175+
return err
176+
}
177+
}
178+
172179
switch opts.Format {
173180
case "json":
174181
content, err = project.MarshalJSON()

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/Microsoft/go-winio v0.6.1
88
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d
99
github.com/buger/goterm v1.0.4
10-
github.com/compose-spec/compose-go/v2 v2.0.3-0.20240407191136-f388192b8a39
10+
github.com/compose-spec/compose-go/v2 v2.0.3-0.20240416141209-60aa6409b2c4
1111
github.com/containerd/console v1.0.4
1212
github.com/containerd/containerd v1.7.13
1313
github.com/davecgh/go-spew v1.1.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4 h1:/inchEIKaYC1Akx+H+g
9090
github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
9191
github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb h1:EDmT6Q9Zs+SbUoc7Ik9EfrFqcylYqgPZ9ANSbTAntnE=
9292
github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb/go.mod h1:ZjrT6AXHbDs86ZSdt/osfBi5qfexBrKUdONk989Wnk4=
93-
github.com/compose-spec/compose-go/v2 v2.0.3-0.20240407191136-f388192b8a39 h1:ZUpnv0xA75X9gy9Y7hjJm51nflGbr+2URaLXBtEic7A=
94-
github.com/compose-spec/compose-go/v2 v2.0.3-0.20240407191136-f388192b8a39/go.mod h1:bEPizBkIojlQ20pi2vNluBa58tevvj0Y18oUSHPyfdc=
93+
github.com/compose-spec/compose-go/v2 v2.0.3-0.20240416141209-60aa6409b2c4 h1:WYiZ9D0WBykHUJLlpt+w7NXX0hy+cQKKdVe7vmsNZvg=
94+
github.com/compose-spec/compose-go/v2 v2.0.3-0.20240416141209-60aa6409b2c4/go.mod h1:bEPizBkIojlQ20pi2vNluBa58tevvj0Y18oUSHPyfdc=
9595
github.com/containerd/cgroups v1.1.0 h1:v8rEWFl6EoqHB+swVNjVoCJE8o3jX7e8nqBGPLaDFBM=
9696
github.com/containerd/cgroups v1.1.0/go.mod h1:6ppBcbh/NOOUU+dMKrykgaBnK9lCIBxHqJDGwsa1mIw=
9797
github.com/containerd/console v1.0.4 h1:F2g4+oChYvBTsASRTz8NP6iIAi97J3TtSAsLbIFn4ro=

pkg/compose/create.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,13 @@ func (s *composeService) create(ctx context.Context, project *types.Project, opt
7777
options.Services = project.ServiceNames()
7878
}
7979

80+
err := project.CheckContainerNameUnicity()
81+
if err != nil {
82+
return err
83+
}
84+
8085
var observedState Containers
81-
observedState, err := s.getContainers(ctx, project.Name, oneOffInclude, true)
86+
observedState, err = s.getContainers(ctx, project.Name, oneOffInclude, true)
8287
if err != nil {
8388
return err
8489
}

pkg/e2e/container_name_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//go:build !windows
2+
// +build !windows
3+
4+
/*
5+
Copyright 2022 Docker Compose CLI authors
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
*/
19+
20+
package e2e
21+
22+
import (
23+
"testing"
24+
25+
"gotest.tools/v3/icmd"
26+
)
27+
28+
func TestUpContainerNameConflict(t *testing.T) {
29+
c := NewParallelCLI(t)
30+
const projectName = "e2e-container_name_conflict"
31+
32+
t.Cleanup(func() {
33+
c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
34+
})
35+
36+
res := c.RunDockerComposeCmdNoCheck(t, "-f", "fixtures/container_name/compose.yaml", "--project-name", projectName, "up")
37+
res.Assert(t, icmd.Expected{ExitCode: 1, Err: `container name "test" is already in use`})
38+
39+
c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
40+
c.RunDockerComposeCmd(t, "-f", "fixtures/container_name/compose.yaml", "--project-name", projectName, "up", "test")
41+
42+
c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
43+
c.RunDockerComposeCmd(t, "-f", "fixtures/container_name/compose.yaml", "--project-name", projectName, "up", "another_test")
44+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
services:
2+
test:
3+
image: alpine
4+
container_name: test
5+
command: /bin/true
6+
7+
another_test:
8+
image: alpine
9+
container_name: test
10+
command: /bin/true

0 commit comments

Comments
 (0)