@@ -18,6 +18,7 @@ import (
18
18
ctxstore "github.com/docker/cli/cli/context/store"
19
19
"github.com/docker/docker/api/types/container"
20
20
"github.com/docker/docker/client"
21
+ "github.com/goccy/go-yaml"
21
22
"github.com/imdario/mergo"
22
23
"github.com/jesseduffield/lazydocker/pkg/commands/ssh"
23
24
"github.com/jesseduffield/lazydocker/pkg/config"
@@ -40,6 +41,7 @@ type DockerCommand struct {
40
41
Config * config.AppConfig
41
42
Client * client.Client
42
43
InDockerComposeProject bool
44
+ ProjectName string
43
45
ErrorChan chan error
44
46
ContainerMutex deadlock.Mutex
45
47
ServiceMutex deadlock.Mutex
@@ -124,6 +126,17 @@ func NewDockerCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.Translat
124
126
if err != nil {
125
127
dockerCommand .InDockerComposeProject = false
126
128
log .Warn (err .Error ())
129
+ } else {
130
+ composeCommand := config .UserConfig .CommandTemplates .DockerCompose
131
+ yamlDockerComposeConfig , err := osCommand .RunCommandWithOutput (fmt .Sprintf ("%s config --format=yaml" , composeCommand ))
132
+
133
+ if err == nil {
134
+ dockerCommand .ProjectName , err = determineProjectName (yamlDockerComposeConfig )
135
+ }
136
+
137
+ if err != nil {
138
+ log .Warn (err .Error ())
139
+ }
127
140
}
128
141
129
142
return dockerCommand , nil
@@ -208,7 +221,7 @@ func (c *DockerCommand) assignContainersToServices(containers []*Container, serv
208
221
L:
209
222
for _ , service := range services {
210
223
for _ , ctr := range containers {
211
- if ! ctr .OneOff && ctr .ServiceName == service .Name {
224
+ if ! ctr .OneOff && ctr .ServiceName == service .Name && ( c . ProjectName == "" || ctr . ProjectName == c . ProjectName ) {
212
225
service .Container = ctr
213
226
continue L
214
227
}
@@ -360,6 +373,24 @@ func (c *DockerCommand) DockerComposeConfig() string {
360
373
return output
361
374
}
362
375
376
+ // determineProjectName tries to the determine the docker compose project name
377
+ func determineProjectName (yamlDockerComposeConfig string ) (string , error ) {
378
+ var projectName string
379
+ var config map [string ]any
380
+
381
+ err := yaml .Unmarshal ([]byte (yamlDockerComposeConfig ), & config )
382
+ if err != nil {
383
+ return projectName , err
384
+ }
385
+
386
+ projectName , ok := config ["name" ].(string )
387
+ if ! ok {
388
+ return projectName , fmt .Errorf ("Incorrect value of docker compose project name in config: %s" , config ["name" ])
389
+ }
390
+
391
+ return projectName , nil
392
+ }
393
+
363
394
// determineDockerHost tries to the determine the docker host that we should connect to
364
395
// in the following order of decreasing precedence:
365
396
// - value of "DOCKER_HOST" environment variable
0 commit comments