@@ -19,8 +19,10 @@ package compose
1919import (
2020 "context"
2121 "fmt"
22+ "os"
2223 "strings"
2324
25+ "github.com/compose-spec/compose-go/v2/dotenv"
2426 "github.com/compose-spec/compose-go/v2/format"
2527 xprogress "github.com/moby/buildkit/util/progress/progressui"
2628 "github.com/sirupsen/logrus"
@@ -44,6 +46,7 @@ type runOptions struct {
4446 Service string
4547 Command []string
4648 environment []string
49+ envFiles []string
4750 Detach bool
4851 Remove bool
4952 noTty bool
@@ -175,6 +178,7 @@ func runCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *
175178 flags := cmd .Flags ()
176179 flags .BoolVarP (& options .Detach , "detach" , "d" , false , "Run container in background and print container ID" )
177180 flags .StringArrayVarP (& options .environment , "env" , "e" , []string {}, "Set environment variables" )
181+ flags .StringArrayVar (& options .envFiles , "env-from-file" , []string {}, "Set environment variables from file" )
178182 flags .StringArrayVarP (& options .labels , "label" , "l" , []string {}, "Add or override a label" )
179183 flags .BoolVar (& options .Remove , "rm" , false , "Automatically remove the container when it exits" )
180184 flags .BoolVarP (& options .noTty , "no-TTY" , "T" , ! dockerCli .Out ().IsTerminal (), "Disable pseudo-TTY allocation (default: auto-detected)" )
@@ -264,6 +268,26 @@ func runRun(ctx context.Context, backend api.Service, project *types.Project, op
264268 buildForRun = & bo
265269 }
266270
271+ environment := types .NewMappingWithEquals (options .environment ).Resolve (os .LookupEnv ).ToMapping ()
272+ for _ , file := range options .envFiles {
273+ f , err := os .Open (file )
274+ if err != nil {
275+ return err
276+ }
277+ vars , err := dotenv .ParseWithLookup (f , func (k string ) (string , bool ) {
278+ value , ok := environment [k ]
279+ return value , ok
280+ })
281+ if err != nil {
282+ return err
283+ }
284+ for k , v := range vars {
285+ if _ , ok := environment [k ]; ! ok {
286+ environment [k ] = v
287+ }
288+ }
289+ }
290+
267291 // start container and attach to container streams
268292 runOpts := api.RunOptions {
269293 Build : buildForRun ,
@@ -278,7 +302,7 @@ func runRun(ctx context.Context, backend api.Service, project *types.Project, op
278302 User : options .user ,
279303 CapAdd : options .capAdd .GetAll (),
280304 CapDrop : options .capDrop .GetAll (),
281- Environment : options . environment ,
305+ Environment : environment . Values () ,
282306 Entrypoint : options .entrypointCmd ,
283307 Labels : labels ,
284308 UseNetworkAliases : options .useAliases ,
0 commit comments