@@ -32,10 +32,13 @@ import (
32
32
"strings"
33
33
"time"
34
34
35
+ compose "github.com/compose-spec/compose-go/cli"
36
+ composeConsts "github.com/compose-spec/compose-go/consts"
35
37
"github.com/docker/docker/api/types"
36
38
"github.com/docker/docker/api/types/container"
37
39
docker "github.com/docker/docker/client"
38
40
"github.com/symfony-cli/terminal"
41
+ "gopkg.in/yaml.v2"
39
42
)
40
43
41
44
var (
@@ -491,7 +494,7 @@ func getEnvValue(env string, key string) string {
491
494
492
495
func (l * Local ) getComposeProjectName () string {
493
496
// https://docs.docker.com/compose/reference/envvars/#compose_project_name
494
- if project := os .Getenv ("COMPOSE_PROJECT_NAME" ); project != "" {
497
+ if project := os .Getenv (composeConsts . ComposeProjectName ); project != "" {
495
498
return project
496
499
}
497
500
@@ -507,13 +510,36 @@ func (l *Local) getComposeProjectName() string {
507
510
if _ , err := os .Stat (filepath .Join (composeDir , ".env" )); err == nil {
508
511
if contents , err := os .ReadFile (filepath .Join (composeDir , ".env" )); err == nil {
509
512
for _ , line := range bytes .Split (contents , []byte ("\n " )) {
510
- if bytes .HasPrefix (line , []byte ("COMPOSE_PROJECT_NAME =" )) {
511
- return string (line [len ("COMPOSE_PROJECT_NAME=" ) :])
513
+ if bytes .HasPrefix (line , []byte (composeConsts . ComposeProjectName + " =" )) {
514
+ return string (line [len (composeConsts . ComposeProjectName ) + 1 :])
512
515
}
513
516
}
514
517
}
515
518
}
516
519
520
+ // Compose project name can be set in every Docker Compose file
521
+ for index , filename := range compose .DefaultFileNames {
522
+ if _ , err := os .Stat (filepath .Join (composeDir , filename )); err != nil {
523
+ continue
524
+ }
525
+
526
+ for _ , filename := range []string {compose .DefaultOverrideFileNames [index ], filename } {
527
+ buf , err := os .ReadFile (filepath .Join (composeDir , filename ))
528
+ if err != nil {
529
+ continue
530
+ }
531
+
532
+ config := struct {
533
+ ProjectName string `yaml:"name"`
534
+ }{}
535
+
536
+ // unmarshall the content of the file to get the project name
537
+ if err := yaml .Unmarshal (buf , & config ); err == nil && config .ProjectName != "" {
538
+ return config .ProjectName
539
+ }
540
+ }
541
+ }
542
+
517
543
return filepath .Base (composeDir )
518
544
}
519
545
@@ -526,7 +552,7 @@ func (l *Local) getComposeDir() string {
526
552
// look for the first dir up with a docker-composer.ya?ml file (in case of a multi-project)
527
553
dir := l .Dir
528
554
for {
529
- for _ , filename := range [] string { " compose.yaml" , "compose.yml" , "docker-compose.yaml" , "docker-compose.yml" } {
555
+ for _ , filename := range compose .DefaultFileNames {
530
556
if _ , err := os .Stat (filepath .Join (dir , filename )); err == nil {
531
557
return dir
532
558
}
0 commit comments