@@ -18,6 +18,8 @@ package compose
1818
1919import (
2020 "context"
21+ "crypto/rand"
22+ "encoding/hex"
2123 "errors"
2224 "fmt"
2325 "os"
@@ -27,9 +29,9 @@ import (
2729 "github.com/compose-spec/compose-go/v2/types"
2830 "github.com/docker/cli/cli"
2931 cmd "github.com/docker/cli/cli/command/container"
32+ "github.com/docker/cli/cli/command/formatter"
3033 "github.com/docker/compose/v2/pkg/api"
3134 "github.com/docker/compose/v2/pkg/progress"
32- "github.com/docker/docker/pkg/stringid"
3335)
3436
3537func (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,
8385 return "" , err
8486 }
8587
86- slug := stringid . GenerateRandomID ()
88+ slug := generateRandomID ()
8789 if service .ContainerName == "" {
88- 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 )
90+ 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 )
8991 }
9092 one := 1
9193 service .Scale = & one
@@ -207,3 +209,14 @@ func (s *composeService) startDependencies(ctx context.Context, project *types.P
207209 }
208210 return nil
209211}
212+
213+ // generateRandomID returns a unique, 64-character ID consisting of a-z, 0-9.
214+ func generateRandomID () string {
215+ b := make ([]byte , 32 )
216+ for {
217+ if _ , err := rand .Read (b ); err != nil {
218+ panic (err ) // This shouldn't happen
219+ }
220+ return hex .EncodeToString (b )
221+ }
222+ }
0 commit comments