@@ -27,6 +27,7 @@ import (
27
27
28
28
"github.com/compose-spec/compose-go/cli"
29
29
"github.com/compose-spec/compose-go/types"
30
+ composegoutils "github.com/compose-spec/compose-go/utils"
30
31
dockercli "github.com/docker/cli/cli"
31
32
"github.com/docker/cli/cli-plugins/manager"
32
33
"github.com/docker/cli/cli/command"
@@ -257,6 +258,10 @@ func RootCommand(dockerCli command.Cli, backend api.Service) *cobra.Command {
257
258
}
258
259
},
259
260
PersistentPreRunE : func (cmd * cobra.Command , args []string ) error {
261
+ err := setEnvWithDotEnv (& opts )
262
+ if err != nil {
263
+ return err
264
+ }
260
265
parent := cmd .Root ()
261
266
if parent != nil {
262
267
parentPrerun := parent .PersistentPreRunE
@@ -333,3 +338,27 @@ func RootCommand(dockerCli command.Cli, backend api.Service) *cobra.Command {
333
338
command .Flags ().MarkHidden ("verbose" ) //nolint:errcheck
334
339
return command
335
340
}
341
+
342
+ func setEnvWithDotEnv (prjOpts * projectOptions ) error {
343
+ options , err := prjOpts .toProjectOptions ()
344
+ if err != nil {
345
+ return compose .WrapComposeError (err )
346
+ }
347
+ workingDir , err := options .GetWorkingDir ()
348
+ if err != nil {
349
+ return err
350
+ }
351
+
352
+ envFromFile , err := cli .GetEnvFromFile (composegoutils .GetAsEqualsMap (os .Environ ()), workingDir , options .EnvFile )
353
+ if err != nil {
354
+ return err
355
+ }
356
+ for k , v := range envFromFile {
357
+ if _ , ok := os .LookupEnv (k ); ! ok {
358
+ if err = os .Setenv (k , v ); err != nil {
359
+ return err
360
+ }
361
+ }
362
+ }
363
+ return nil
364
+ }
0 commit comments