diff --git a/cmd/compose/bridge.go b/cmd/compose/bridge.go index d0e5e2083..14d48c6b6 100644 --- a/cmd/compose/bridge.go +++ b/cmd/compose/bridge.go @@ -23,7 +23,7 @@ import ( "github.com/distribution/reference" "github.com/docker/cli/cli/command" - "github.com/docker/docker/pkg/stringid" + stringid "github.com/docker/cli/cli/command/formatter" "github.com/docker/go-units" "github.com/moby/moby/api/types/image" "github.com/spf13/cobra" diff --git a/cmd/compose/images.go b/cmd/compose/images.go index 5544204c8..f8e5662fa 100644 --- a/cmd/compose/images.go +++ b/cmd/compose/images.go @@ -27,7 +27,7 @@ import ( "github.com/containerd/platforms" "github.com/docker/cli/cli/command" - "github.com/docker/docker/pkg/stringid" + stringid "github.com/docker/cli/cli/command/formatter" "github.com/docker/go-units" "github.com/spf13/cobra" diff --git a/cmd/formatter/container.go b/cmd/formatter/container.go index 354c03d44..a0e74cb31 100644 --- a/cmd/formatter/container.go +++ b/cmd/formatter/container.go @@ -24,7 +24,6 @@ import ( "github.com/docker/cli/cli/command/formatter" "github.com/docker/compose/v2/pkg/api" - "github.com/docker/docker/pkg/stringid" "github.com/docker/go-units" "github.com/moby/moby/api/types/container" ) @@ -137,7 +136,7 @@ func (c *ContainerContext) MarshalJSON() ([]byte, error) { // option being set, the full or truncated ID is returned. func (c *ContainerContext) ID() string { if c.trunc { - return stringid.TruncateID(c.c.ID) + return formatter.TruncateID(c.c.ID) } return c.c.ID } diff --git a/pkg/compose/run.go b/pkg/compose/run.go index 242f2fc1c..3670e2950 100644 --- a/pkg/compose/run.go +++ b/pkg/compose/run.go @@ -18,6 +18,8 @@ package compose import ( "context" + "crypto/rand" + "encoding/hex" "errors" "fmt" "os" @@ -27,9 +29,9 @@ import ( "github.com/compose-spec/compose-go/v2/types" "github.com/docker/cli/cli" cmd "github.com/docker/cli/cli/command/container" + "github.com/docker/cli/cli/command/formatter" "github.com/docker/compose/v2/pkg/api" "github.com/docker/compose/v2/pkg/progress" - "github.com/docker/docker/pkg/stringid" ) func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts api.RunOptions) (int, error) { @@ -83,9 +85,9 @@ func (s *composeService) prepareRun(ctx context.Context, project *types.Project, return "", err } - slug := stringid.GenerateRandomID() + slug := generateRandomID() if service.ContainerName == "" { - service.ContainerName = fmt.Sprintf("%[1]s%[4]s%[2]s%[4]srun%[4]s%[3]s", project.Name, service.Name, stringid.TruncateID(slug), api.Separator) + service.ContainerName = fmt.Sprintf("%[1]s%[4]s%[2]s%[4]srun%[4]s%[3]s", project.Name, service.Name, formatter.TruncateID(slug), api.Separator) } one := 1 service.Scale = &one @@ -207,3 +209,14 @@ func (s *composeService) startDependencies(ctx context.Context, project *types.P } return nil } + +// generateRandomID returns a unique, 64-character ID consisting of a-z, 0-9. +func generateRandomID() string { + b := make([]byte, 32) + for { + if _, err := rand.Read(b); err != nil { + panic(err) // This shouldn't happen + } + return hex.EncodeToString(b) + } +}