1
0
mirror of https://github.com/docker/compose.git synced 2025-04-08 17:05:13 +02:00

Strip project prefix from docker-compose up output

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2023-11-15 14:41:35 +01:00 committed by Nicolas De loof
parent c16943609c
commit 9c4efbdd92
4 changed files with 15 additions and 14 deletions

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

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

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

@ -17,7 +17,6 @@
package e2e
import (
"fmt"
"strings"
"testing"
"time"
@ -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 {