Skip to content

Commit 150fd4b

Browse files
committed
use '-' as separator by default for image name
Signed-off-by: Guillaume Lours <[email protected]>
1 parent b49bd7c commit 150fd4b

File tree

10 files changed

+41
-41
lines changed

10 files changed

+41
-41
lines changed

cmd/compose/compose.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,15 @@ func (o *projectOptions) toProject(services []string, po ...cli.ProjectOptionsFn
159159
return nil, compose.WrapComposeError(err)
160160
}
161161

162+
if o.Compatibility || utils.StringToBool(options.Environment["COMPOSE_COMPATIBILITY"]) {
163+
api.Separator = "_"
164+
}
165+
162166
project, err := cli.ProjectFromOptions(options)
163167
if err != nil {
164168
return nil, compose.WrapComposeError(err)
165169
}
166170

167-
if o.Compatibility || utils.StringToBool(project.Environment["COMPOSE_COMPATIBILITY"]) {
168-
compose.Separator = "_"
169-
}
170-
171171
ef := o.EnvFile
172172
if ef != "" && !filepath.IsAbs(ef) {
173173
ef, err = filepath.Abs(ef)

pkg/api/api.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,11 +446,14 @@ const (
446446
UserCancel
447447
)
448448

449+
// Separator is used for naming components
450+
var Separator = "-"
451+
449452
// GetImageNameOrDefault computes the default image name for a service, used to tag built images
450453
func GetImageNameOrDefault(service types.ServiceConfig, projectName string) string {
451454
imageName := service.Image
452455
if imageName == "" {
453-
imageName = projectName + "_" + service.Name
456+
imageName = projectName + Separator + service.Name
454457
}
455458
return imageName
456459
}

pkg/compose/compose.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ import (
3636
"github.com/sanathkr/go-yaml"
3737
)
3838

39-
// Separator is used for naming components
40-
var Separator = "-"
41-
4239
// NewComposeService create a local implementation of the compose.Service API
4340
func NewComposeService(dockerCli command.Cli) api.Service {
4441
return &composeService{

pkg/compose/convergence.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ func mustRecreate(expected types.ServiceConfig, actual moby.Container, policy st
261261
}
262262

263263
func getContainerName(projectName string, service types.ServiceConfig, number int) string {
264-
name := strings.Join([]string{projectName, service.Name, strconv.Itoa(number)}, Separator)
264+
name := strings.Join([]string{projectName, service.Name, strconv.Itoa(number)}, api.Separator)
265265
if service.ContainerName != "" {
266266
name = service.ContainerName
267267
}
@@ -553,8 +553,8 @@ func (s composeService) getLinks(ctx context.Context, projectName string, servic
553553
containerName := getCanonicalContainerName(c)
554554
links = append(links,
555555
format(containerName, linkName),
556-
format(containerName, linkServiceName+Separator+strconv.Itoa(number)),
557-
format(containerName, strings.Join([]string{projectName, linkServiceName, strconv.Itoa(number)}, Separator)),
556+
format(containerName, linkServiceName+api.Separator+strconv.Itoa(number)),
557+
format(containerName, strings.Join([]string{projectName, linkServiceName, strconv.Itoa(number)}, api.Separator)),
558558
)
559559
}
560560
}
@@ -568,7 +568,7 @@ func (s composeService) getLinks(ctx context.Context, projectName string, servic
568568
containerName := getCanonicalContainerName(c)
569569
links = append(links,
570570
format(containerName, service.Name),
571-
format(containerName, strings.TrimPrefix(containerName, projectName+Separator)),
571+
format(containerName, strings.TrimPrefix(containerName, projectName+api.Separator)),
572572
format(containerName, containerName),
573573
)
574574
}

pkg/compose/create_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func TestBuildVolumeMount(t *testing.T) {
8080

8181
func TestServiceImageName(t *testing.T) {
8282
assert.Equal(t, api.GetImageNameOrDefault(composetypes.ServiceConfig{Image: "myImage"}, "myProject"), "myImage")
83-
assert.Equal(t, api.GetImageNameOrDefault(composetypes.ServiceConfig{Name: "aService"}, "myProject"), "myProject_aService")
83+
assert.Equal(t, api.GetImageNameOrDefault(composetypes.ServiceConfig{Name: "aService"}, "myProject"), "myProject-aService")
8484
}
8585

8686
func TestPrepareNetworkLabels(t *testing.T) {

pkg/e2e/build_test.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,30 @@ func TestLocalComposeBuild(t *testing.T) {
3131

3232
t.Run("build named and unnamed images", func(t *testing.T) {
3333
// ensure local test run does not reuse previously build image
34-
c.RunDockerOrExitError(t, "rmi", "build-test_nginx")
34+
c.RunDockerOrExitError(t, "rmi", "build-test-nginx")
3535
c.RunDockerOrExitError(t, "rmi", "custom-nginx")
3636

3737
res := c.RunDockerComposeCmd(t, "--project-directory", "fixtures/build-test", "build")
3838

3939
res.Assert(t, icmd.Expected{Out: "COPY static /usr/share/nginx/html"})
40-
c.RunDockerCmd(t, "image", "inspect", "build-test_nginx")
40+
c.RunDockerCmd(t, "image", "inspect", "build-test-nginx")
4141
c.RunDockerCmd(t, "image", "inspect", "custom-nginx")
4242
})
4343

4444
t.Run("build with build-arg", func(t *testing.T) {
4545
// ensure local test run does not reuse previously build image
46-
c.RunDockerOrExitError(t, "rmi", "build-test_nginx")
46+
c.RunDockerOrExitError(t, "rmi", "build-test-nginx")
4747
c.RunDockerOrExitError(t, "rmi", "custom-nginx")
4848

4949
c.RunDockerComposeCmd(t, "--project-directory", "fixtures/build-test", "build", "--build-arg", "FOO=BAR")
5050

51-
res := c.RunDockerCmd(t, "image", "inspect", "build-test_nginx")
51+
res := c.RunDockerCmd(t, "image", "inspect", "build-test-nginx")
5252
res.Assert(t, icmd.Expected{Out: `"FOO": "BAR"`})
5353
})
5454

5555
t.Run("build with build-arg set by env", func(t *testing.T) {
5656
// ensure local test run does not reuse previously build image
57-
c.RunDockerOrExitError(t, "rmi", "build-test_nginx")
57+
c.RunDockerOrExitError(t, "rmi", "build-test-nginx")
5858
c.RunDockerOrExitError(t, "rmi", "custom-nginx")
5959

6060
icmd.RunCmd(c.NewDockerComposeCmd(t,
@@ -67,20 +67,20 @@ func TestLocalComposeBuild(t *testing.T) {
6767
cmd.Env = append(cmd.Env, "FOO=BAR")
6868
})
6969

70-
res := c.RunDockerCmd(t, "image", "inspect", "build-test_nginx")
70+
res := c.RunDockerCmd(t, "image", "inspect", "build-test-nginx")
7171
res.Assert(t, icmd.Expected{Out: `"FOO": "BAR"`})
7272
})
7373

7474
t.Run("build with multiple build-args ", func(t *testing.T) {
7575
// ensure local test run does not reuse previously build image
76-
c.RunDockerOrExitError(t, "rmi", "-f", "multi-args_multiargs")
76+
c.RunDockerOrExitError(t, "rmi", "-f", "multi-args-multiargs")
7777
cmd := c.NewDockerComposeCmd(t, "--project-directory", "fixtures/build-test/multi-args", "build")
7878

7979
icmd.RunCmd(cmd, func(cmd *icmd.Cmd) {
8080
cmd.Env = append(cmd.Env, "DOCKER_BUILDKIT=0")
8181
})
8282

83-
res := c.RunDockerCmd(t, "image", "inspect", "multi-args_multiargs")
83+
res := c.RunDockerCmd(t, "image", "inspect", "multi-args-multiargs")
8484
res.Assert(t, icmd.Expected{Out: `"RESULT": "SUCCESS"`})
8585
})
8686

@@ -131,7 +131,7 @@ func TestLocalComposeBuild(t *testing.T) {
131131
})
132132

133133
t.Run("build as part of up", func(t *testing.T) {
134-
c.RunDockerOrExitError(t, "rmi", "build-test_nginx")
134+
c.RunDockerOrExitError(t, "rmi", "build-test-nginx")
135135
c.RunDockerOrExitError(t, "rmi", "custom-nginx")
136136

137137
res := c.RunDockerComposeCmd(t, "--project-directory", "fixtures/build-test", "up", "-d")
@@ -145,7 +145,7 @@ func TestLocalComposeBuild(t *testing.T) {
145145
output := HTTPGetWithRetry(t, "http://localhost:8070", http.StatusOK, 2*time.Second, 20*time.Second)
146146
assert.Assert(t, strings.Contains(output, "Hello from Nginx container"))
147147

148-
c.RunDockerCmd(t, "image", "inspect", "build-test_nginx")
148+
c.RunDockerCmd(t, "image", "inspect", "build-test-nginx")
149149
c.RunDockerCmd(t, "image", "inspect", "custom-nginx")
150150
})
151151

@@ -164,7 +164,7 @@ func TestLocalComposeBuild(t *testing.T) {
164164

165165
t.Run("cleanup build project", func(t *testing.T) {
166166
c.RunDockerComposeCmd(t, "--project-directory", "fixtures/build-test", "down")
167-
c.RunDockerCmd(t, "rmi", "build-test_nginx")
167+
c.RunDockerCmd(t, "rmi", "build-test-nginx")
168168
c.RunDockerCmd(t, "rmi", "custom-nginx")
169169
})
170170
}
@@ -216,19 +216,19 @@ func TestBuildImageDependencies(t *testing.T) {
216216
t.Cleanup(resetState)
217217

218218
// the image should NOT exist now
219-
res := cli.RunDockerOrExitError(t, "image", "inspect", "build-dependencies_service")
219+
res := cli.RunDockerOrExitError(t, "image", "inspect", "build-dependencies-service")
220220
res.Assert(t, icmd.Expected{
221221
ExitCode: 1,
222-
Err: "Error: No such image: build-dependencies_service",
222+
Err: "Error: No such image: build-dependencies-service",
223223
})
224224

225225
res = cli.RunDockerComposeCmd(t, "build")
226226
t.Log(res.Combined())
227227

228228
res = cli.RunDockerCmd(t,
229229
"image", "inspect", "--format={{ index .RepoTags 0 }}",
230-
"build-dependencies_service")
231-
res.Assert(t, icmd.Expected{Out: "build-dependencies_service:latest"})
230+
"build-dependencies-service")
231+
res.Assert(t, icmd.Expected{Out: "build-dependencies-service:latest"})
232232
}
233233

234234
t.Run("ClassicBuilder", func(t *testing.T) {

pkg/e2e/fixtures/project-volume-bind-test/docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ services:
33
image: nginx
44
container_name: frontend
55
volumes:
6-
- project_data:/data
6+
- project-data:/data
77

88
volumes:
9-
project_data:
9+
project-data:
1010
driver: local
1111
driver_opts:
1212
type: none

pkg/e2e/networks_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ func TestNetworks(t *testing.T) {
3030
// fixture is shared with TestNetworkModes and is not safe to run concurrently
3131
c := NewCLI(t)
3232

33-
const projectName = "network_e2e"
33+
const projectName = "network-e2e"
3434

3535
t.Run("ensure we do not reuse previous networks", func(t *testing.T) {
36-
c.RunDockerOrExitError(t, "network", "rm", projectName+"_dbnet")
36+
c.RunDockerOrExitError(t, "network", "rm", projectName+"-dbnet")
3737
c.RunDockerOrExitError(t, "network", "rm", "microservices")
3838
})
3939

@@ -125,7 +125,7 @@ func TestIPAMConfig(t *testing.T) {
125125
const projectName = "ipam_e2e"
126126

127127
t.Run("ensure we do not reuse previous networks", func(t *testing.T) {
128-
c.RunDockerOrExitError(t, "network", "rm", projectName+"_default")
128+
c.RunDockerOrExitError(t, "network", "rm", projectName+"-default")
129129
})
130130

131131
t.Run("up", func(t *testing.T) {

pkg/e2e/scan_message_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,24 @@ func TestDisplayScanMessageAfterBuild(t *testing.T) {
3737
t.Run("display on compose build", func(t *testing.T) {
3838
res := c.RunDockerComposeCmd(t, "-f", "fixtures/simple-build-test/compose.yaml", "-p",
3939
"scan-msg-test-compose-build", "build")
40-
defer c.RunDockerOrExitError(t, "rmi", "-f", "scan-msg-test-compose-build_nginx")
40+
defer c.RunDockerOrExitError(t, "rmi", "-f", "scan-msg-test-compose-build-nginx")
4141
res.Assert(t, icmd.Expected{Err: utils.ScanSuggestMsg})
4242
})
4343

4444
t.Run("do not display on compose build with quiet flag", func(t *testing.T) {
4545
res := c.RunDockerComposeCmd(t, "-f", "fixtures/simple-build-test/compose.yaml", "-p", "scan-msg-test-quiet",
4646
"build", "--quiet")
4747
assert.Assert(t, !strings.Contains(res.Combined(), "docker scan"), res.Combined())
48-
res = c.RunDockerCmd(t, "rmi", "-f", "scan-msg-test-quiet_nginx")
48+
res = c.RunDockerCmd(t, "rmi", "-f", "scan-msg-test-quiet-nginx")
4949
assert.Assert(t, !strings.Contains(res.Combined(), "No such image"))
5050

5151
res = c.RunDockerComposeCmd(t, "-f", "fixtures/simple-build-test/compose.yaml", "-p", "scan-msg-test-q",
5252
"build", "-q")
53-
defer c.RunDockerOrExitError(t, "rmi", "-f", "scan-msg-test-q_nginx")
53+
defer c.RunDockerOrExitError(t, "rmi", "-f", "scan-msg-test-q-nginx")
5454
assert.Assert(t, !strings.Contains(res.Combined(), "docker scan"), res.Combined())
5555
})
5656

57-
_ = c.RunDockerOrExitError(t, "rmi", "scan-msg-test_nginx")
57+
_ = c.RunDockerOrExitError(t, "rmi", "scan-msg-test-nginx")
5858

5959
t.Run("display on compose up if image is built", func(t *testing.T) {
6060
res := c.RunDockerComposeCmd(t, "-f", "fixtures/simple-build-test/compose.yaml", "-p", "scan-msg-test", "up",

pkg/e2e/volumes_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ func TestLocalComposeVolume(t *testing.T) {
3535

3636
t.Run("up with build and no image name, volume", func(t *testing.T) {
3737
// ensure local test run does not reuse previously build image
38-
c.RunDockerOrExitError(t, "rmi", "compose-e2e-volume_nginx")
39-
c.RunDockerOrExitError(t, "volume", "rm", projectName+"_staticVol")
38+
c.RunDockerOrExitError(t, "rmi", "compose-e2e-volume-nginx")
39+
c.RunDockerOrExitError(t, "volume", "rm", projectName+"-staticVol")
4040
c.RunDockerOrExitError(t, "volume", "rm", "myvolume")
4141
c.RunDockerComposeCmd(t, "--project-directory", "fixtures/volume-test", "--project-name", projectName, "up",
4242
"-d")
@@ -88,7 +88,7 @@ func TestLocalComposeVolume(t *testing.T) {
8888
t.Run("cleanup volume project", func(t *testing.T) {
8989
c.RunDockerComposeCmd(t, "--project-name", projectName, "down", "--volumes")
9090
ls := c.RunDockerCmd(t, "volume", "ls").Stdout()
91-
assert.Assert(t, !strings.Contains(ls, projectName+"_staticVol"))
91+
assert.Assert(t, !strings.Contains(ls, projectName+"-staticVol"))
9292
assert.Assert(t, !strings.Contains(ls, "myvolume"))
9393
})
9494
}
@@ -107,7 +107,7 @@ func TestProjectVolumeBind(t *testing.T) {
107107

108108
c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
109109

110-
c.RunDockerOrExitError(t, "volume", "rm", "-f", projectName+"_project_data").Assert(t, icmd.Success)
110+
c.RunDockerOrExitError(t, "volume", "rm", "-f", projectName+"-project-data").Assert(t, icmd.Success)
111111
cmd := c.NewCmdWithEnv([]string{"TEST_DIR=" + tmpDir},
112112
"docker", "compose", "--project-directory", "fixtures/project-volume-bind-test", "--project-name", projectName, "up", "-d")
113113
icmd.RunCmd(cmd).Assert(t, icmd.Success)

0 commit comments

Comments
 (0)