Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f25f81a

Browse filesBrowse files
authored
Merge pull request #515 from tucksaun/refacto/docker-compose-project-name
refacto: improve Docker Compose project name determination
2 parents 12013a9 + 903639a commit f25f81a
Copy full SHA for f25f81a

File tree

1 file changed

+30
-4
lines changed
Filter options

1 file changed

+30
-4
lines changed

‎envs/docker.go

Copy file name to clipboardExpand all lines: envs/docker.go
+30-4Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,13 @@ import (
3232
"strings"
3333
"time"
3434

35+
compose "github.com/compose-spec/compose-go/cli"
36+
composeConsts "github.com/compose-spec/compose-go/consts"
3537
"github.com/docker/docker/api/types"
3638
"github.com/docker/docker/api/types/container"
3739
docker "github.com/docker/docker/client"
3840
"github.com/symfony-cli/terminal"
41+
"gopkg.in/yaml.v2"
3942
)
4043

4144
var (
@@ -491,7 +494,7 @@ func getEnvValue(env string, key string) string {
491494

492495
func (l *Local) getComposeProjectName() string {
493496
// 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 != "" {
495498
return project
496499
}
497500

@@ -507,13 +510,36 @@ func (l *Local) getComposeProjectName() string {
507510
if _, err := os.Stat(filepath.Join(composeDir, ".env")); err == nil {
508511
if contents, err := os.ReadFile(filepath.Join(composeDir, ".env")); err == nil {
509512
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:])
512515
}
513516
}
514517
}
515518
}
516519

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+
517543
return filepath.Base(composeDir)
518544
}
519545

@@ -526,7 +552,7 @@ func (l *Local) getComposeDir() string {
526552
// look for the first dir up with a docker-composer.ya?ml file (in case of a multi-project)
527553
dir := l.Dir
528554
for {
529-
for _, filename := range []string{"compose.yaml", "compose.yml", "docker-compose.yaml", "docker-compose.yml"} {
555+
for _, filename := range compose.DefaultFileNames {
530556
if _, err := os.Stat(filepath.Join(dir, filename)); err == nil {
531557
return dir
532558
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.