Skip to content

Commit 9c4efbd

Browse files
committed
Strip project prefix from docker-compose up output
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent c169436 commit 9c4efbd

File tree

4 files changed

+15
-14
lines changed

4 files changed

+15
-14
lines changed

pkg/compose/compose.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,14 @@ func getCanonicalContainerName(c moby.Container) string {
136136
}
137137

138138
func getContainerNameWithoutProject(c moby.Container) string {
139-
name := getCanonicalContainerName(c)
140139
project := c.Labels[api.ProjectLabel]
141-
prefix := fmt.Sprintf("%s_%s_", project, c.Labels[api.ServiceLabel])
142-
if strings.HasPrefix(name, prefix) {
143-
return name[len(project)+1:]
140+
defaultName := getDefaultContainerName(project, c.Labels[api.ServiceLabel], c.Labels[api.ContainerNumberLabel])
141+
name := getCanonicalContainerName(c)
142+
if name != defaultName {
143+
// service declares a custom container_name
144+
return name
144145
}
145-
return name
146+
return name[len(project)+1:]
146147
}
147148

148149
func (s *composeService) Config(ctx context.Context, project *types.Project, options api.ConfigOptions) ([]byte, error) {

pkg/compose/convergence.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,17 @@ func mustRecreate(expected types.ServiceConfig, actual moby.Container, policy st
287287
}
288288

289289
func getContainerName(projectName string, service types.ServiceConfig, number int) string {
290-
name := strings.Join([]string{projectName, service.Name, strconv.Itoa(number)}, api.Separator)
290+
name := getDefaultContainerName(projectName, service.Name, strconv.Itoa(number))
291291
if service.ContainerName != "" {
292292
name = service.ContainerName
293293
}
294294
return name
295295
}
296296

297+
func getDefaultContainerName(projectName, serviceName, index string) string {
298+
return strings.Join([]string{projectName, serviceName, index}, api.Separator)
299+
}
300+
297301
func getContainerProgressName(container moby.Container) string {
298302
return "Container " + getCanonicalContainerName(container)
299303
}

pkg/e2e/build_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ func TestBuildPlatformsWithCorrectBuildxConfig(t *testing.T) {
333333
t.Run("multi-arch up --build", func(t *testing.T) {
334334
res := c.RunDockerComposeCmdNoCheck(t, "--project-directory", "fixtures/build-test/platforms", "up", "--build")
335335
assert.NilError(t, res.Error, res.Stderr())
336-
res.Assert(t, icmd.Expected{Out: "platforms-platforms-1 exited with code 0"})
336+
res.Assert(t, icmd.Expected{Out: "platforms-1 exited with code 0"})
337337
})
338338

339339
t.Run("use DOCKER_DEFAULT_PLATFORM value when up --build", func(t *testing.T) {

pkg/e2e/logs_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package e2e
1818

1919
import (
20-
"fmt"
2120
"strings"
2221
"testing"
2322
"time"
@@ -75,18 +74,15 @@ func TestLocalComposeLogsFollow(t *testing.T) {
7574
_ = res.Cmd.Process.Kill()
7675
})
7776

78-
expected := fmt.Sprintf("%s-ping-1 ", projectName)
79-
poll.WaitOn(t, expectOutput(res, expected), poll.WithDelay(100*time.Millisecond), poll.WithTimeout(1*time.Second))
77+
poll.WaitOn(t, expectOutput(res, "ping-1 "), poll.WithDelay(100*time.Millisecond), poll.WithTimeout(1*time.Second))
8078

8179
c.RunDockerComposeCmd(t, "-f", "./fixtures/logs-test/compose.yaml", "--project-name", projectName, "up", "-d")
8280

83-
expected = fmt.Sprintf("%s-hello-1 ", projectName)
84-
poll.WaitOn(t, expectOutput(res, expected), poll.WithDelay(100*time.Millisecond), poll.WithTimeout(1*time.Second))
81+
poll.WaitOn(t, expectOutput(res, "hello-1 "), poll.WithDelay(100*time.Millisecond), poll.WithTimeout(1*time.Second))
8582

8683
c.RunDockerComposeCmd(t, "-f", "./fixtures/logs-test/compose.yaml", "--project-name", projectName, "up", "-d", "--scale", "ping=2", "ping")
8784

88-
expected = fmt.Sprintf("%s-ping-2 ", projectName)
89-
poll.WaitOn(t, expectOutput(res, expected), poll.WithDelay(100*time.Millisecond), poll.WithTimeout(20*time.Second))
85+
poll.WaitOn(t, expectOutput(res, "ping-2 "), poll.WithDelay(100*time.Millisecond), poll.WithTimeout(20*time.Second))
9086
}
9187

9288
func expectOutput(res *icmd.Result, expected string) func(t poll.LogT) poll.Result {

0 commit comments

Comments
 (0)