Skip to content

Commit

Permalink
Strip project prefix from docker-compose up output
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
  • Loading branch information
ndeloof committed Nov 15, 2023
1 parent c169436 commit 9c4efbd
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
11 changes: 6 additions & 5 deletions pkg/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,14 @@ func getCanonicalContainerName(c moby.Container) string {
}

func getContainerNameWithoutProject(c moby.Container) string {
name := getCanonicalContainerName(c)
project := c.Labels[api.ProjectLabel]
prefix := fmt.Sprintf("%s_%s_", project, c.Labels[api.ServiceLabel])
if strings.HasPrefix(name, prefix) {
return name[len(project)+1:]
defaultName := getDefaultContainerName(project, c.Labels[api.ServiceLabel], c.Labels[api.ContainerNumberLabel])
name := getCanonicalContainerName(c)
if name != defaultName {
// service declares a custom container_name
return name
}
return name
return name[len(project)+1:]
}

func (s *composeService) Config(ctx context.Context, project *types.Project, options api.ConfigOptions) ([]byte, error) {
Expand Down
6 changes: 5 additions & 1 deletion pkg/compose/convergence.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,13 +287,17 @@ func mustRecreate(expected types.ServiceConfig, actual moby.Container, policy st
}

func getContainerName(projectName string, service types.ServiceConfig, number int) string {
name := strings.Join([]string{projectName, service.Name, strconv.Itoa(number)}, api.Separator)
name := getDefaultContainerName(projectName, service.Name, strconv.Itoa(number))
if service.ContainerName != "" {
name = service.ContainerName
}
return name
}

func getDefaultContainerName(projectName, serviceName, index string) string {
return strings.Join([]string{projectName, serviceName, index}, api.Separator)
}

func getContainerProgressName(container moby.Container) string {
return "Container " + getCanonicalContainerName(container)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/e2e/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ func TestBuildPlatformsWithCorrectBuildxConfig(t *testing.T) {
t.Run("multi-arch up --build", func(t *testing.T) {
res := c.RunDockerComposeCmdNoCheck(t, "--project-directory", "fixtures/build-test/platforms", "up", "--build")
assert.NilError(t, res.Error, res.Stderr())
res.Assert(t, icmd.Expected{Out: "platforms-platforms-1 exited with code 0"})
res.Assert(t, icmd.Expected{Out: "platforms-1 exited with code 0"})
})

t.Run("use DOCKER_DEFAULT_PLATFORM value when up --build", func(t *testing.T) {
Expand Down
10 changes: 3 additions & 7 deletions pkg/e2e/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package e2e

import (
"fmt"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -75,18 +74,15 @@ func TestLocalComposeLogsFollow(t *testing.T) {
_ = res.Cmd.Process.Kill()
})

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

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

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

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

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

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

0 comments on commit 9c4efbd

Please sign in to comment.