@@ -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
@@ -116,6 +119,29 @@ func (options runOptions) apply(project *types.Project) (*types.Project, error)
116119 return project , nil
117120}
118121
122+ func (options runOptions ) getEnvironment () (types.Mapping , error ) {
123+ environment := types .NewMappingWithEquals (options .environment ).Resolve (os .LookupEnv ).ToMapping ()
124+ for _ , file := range options .envFiles {
125+ f , err := os .Open (file )
126+ if err != nil {
127+ return nil , err
128+ }
129+ vars , err := dotenv .ParseWithLookup (f , func (k string ) (string , bool ) {
130+ value , ok := environment [k ]
131+ return value , ok
132+ })
133+ if err != nil {
134+ return nil , nil
135+ }
136+ for k , v := range vars {
137+ if _ , ok := environment [k ]; ! ok {
138+ environment [k ] = v
139+ }
140+ }
141+ }
142+ return environment , nil
143+ }
144+
119145func runCommand (p * ProjectOptions , dockerCli command.Cli , backend api.Service ) * cobra.Command {
120146 options := runOptions {
121147 composeOptions : & composeOptions {
@@ -175,6 +201,7 @@ func runCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *
175201 flags := cmd .Flags ()
176202 flags .BoolVarP (& options .Detach , "detach" , "d" , false , "Run container in background and print container ID" )
177203 flags .StringArrayVarP (& options .environment , "env" , "e" , []string {}, "Set environment variables" )
204+ flags .StringArrayVar (& options .envFiles , "env-from-file" , []string {}, "Set environment variables from file" )
178205 flags .StringArrayVarP (& options .labels , "label" , "l" , []string {}, "Add or override a label" )
179206 flags .BoolVar (& options .Remove , "rm" , false , "Automatically remove the container when it exits" )
180207 flags .BoolVarP (& options .noTty , "no-TTY" , "T" , ! dockerCli .Out ().IsTerminal (), "Disable pseudo-TTY allocation (default: auto-detected)" )
@@ -264,6 +291,11 @@ func runRun(ctx context.Context, backend api.Service, project *types.Project, op
264291 buildForRun = & bo
265292 }
266293
294+ environment , err := options .getEnvironment ()
295+ if err != nil {
296+ return err
297+ }
298+
267299 // start container and attach to container streams
268300 runOpts := api.RunOptions {
269301 Build : buildForRun ,
@@ -278,7 +310,7 @@ func runRun(ctx context.Context, backend api.Service, project *types.Project, op
278310 User : options .user ,
279311 CapAdd : options .capAdd .GetAll (),
280312 CapDrop : options .capDrop .GetAll (),
281- Environment : options . environment ,
313+ Environment : environment . Values () ,
282314 Entrypoint : options .entrypointCmd ,
283315 Labels : labels ,
284316 UseNetworkAliases : options .useAliases ,
0 commit comments