@@ -23,6 +23,7 @@ import (
23
23
"bytes"
24
24
"context"
25
25
"fmt"
26
+ "gopkg.in/yaml.v2"
26
27
"net"
27
28
"net/url"
28
29
"os"
@@ -33,6 +34,8 @@ import (
33
34
"strings"
34
35
"time"
35
36
37
+ compose "github.com/compose-spec/compose-go/cli"
38
+ composeConsts "github.com/compose-spec/compose-go/consts"
36
39
"github.com/docker/docker/api/types"
37
40
"github.com/docker/docker/api/types/container"
38
41
docker "github.com/docker/docker/client"
@@ -483,7 +486,7 @@ func getEnvValue(env string, key string) string {
483
486
484
487
func (l * Local ) getComposeProjectName () string {
485
488
// https://docs.docker.com/compose/reference/envvars/#compose_project_name
486
- if project := os .Getenv ("COMPOSE_PROJECT_NAME" ); project != "" {
489
+ if project := os .Getenv (composeConsts . ComposeProjectName ); project != "" {
487
490
return project
488
491
}
489
492
@@ -499,13 +502,36 @@ func (l *Local) getComposeProjectName() string {
499
502
if _ , err := os .Stat (filepath .Join (composeDir , ".env" )); err == nil {
500
503
if contents , err := os .ReadFile (filepath .Join (composeDir , ".env" )); err == nil {
501
504
for _ , line := range bytes .Split (contents , []byte ("\n " )) {
502
- if bytes .HasPrefix (line , []byte ("COMPOSE_PROJECT_NAME =" )) {
503
- return string (line [len ("COMPOSE_PROJECT_NAME=" ) :])
505
+ if bytes .HasPrefix (line , []byte (composeConsts . ComposeProjectName + " =" )) {
506
+ return string (line [len (composeConsts . ComposeProjectName ) + 1 :])
504
507
}
505
508
}
506
509
}
507
510
}
508
511
512
+ // Compose project name can be set in every Docker Compose file
513
+ for index , filename := range compose .DefaultFileNames {
514
+ if _ , err := os .Stat (filepath .Join (composeDir , filename )); err != nil {
515
+ continue
516
+ }
517
+
518
+ for _ , filename := range []string {compose .DefaultOverrideFileNames [index ], filename } {
519
+ buf , err := os .ReadFile (filepath .Join (composeDir , filename ))
520
+ if err != nil {
521
+ continue
522
+ }
523
+
524
+ config := struct {
525
+ ProjectName string `yaml:"name"`
526
+ }{}
527
+
528
+ // unmarshall the content of the file to get the project name
529
+ if err := yaml .Unmarshal (buf , & config ); err == nil && config .ProjectName != "" {
530
+ return config .ProjectName
531
+ }
532
+ }
533
+ }
534
+
509
535
return filepath .Base (composeDir )
510
536
}
511
537
@@ -518,7 +544,7 @@ func (l *Local) getComposeDir() string {
518
544
// look for the first dir up with a docker-composer.ya?ml file (in case of a multi-project)
519
545
dir := l .Dir
520
546
for {
521
- for _ , filename := range [] string { " compose.yaml" , "compose.yml" , "docker-compose.yaml" , "docker-compose.yml" } {
547
+ for _ , filename := range compose .DefaultFileNames {
522
548
if _ , err := os .Stat (filepath .Join (dir , filename )); err == nil {
523
549
return dir
524
550
}
0 commit comments