@@ -19,8 +19,10 @@ package compose
19
19
import (
20
20
"context"
21
21
"fmt"
22
+ "os"
22
23
"strings"
23
24
25
+ "github.com/compose-spec/compose-go/v2/dotenv"
24
26
"github.com/compose-spec/compose-go/v2/format"
25
27
xprogress "github.com/moby/buildkit/util/progress/progressui"
26
28
"github.com/sirupsen/logrus"
@@ -44,6 +46,7 @@ type runOptions struct {
44
46
Service string
45
47
Command []string
46
48
environment []string
49
+ envFiles []string
47
50
Detach bool
48
51
Remove bool
49
52
noTty bool
@@ -116,6 +119,29 @@ func (options runOptions) apply(project *types.Project) (*types.Project, error)
116
119
return project , nil
117
120
}
118
121
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
+
119
145
func runCommand (p * ProjectOptions , dockerCli command.Cli , backend api.Service ) * cobra.Command {
120
146
options := runOptions {
121
147
composeOptions : & composeOptions {
@@ -175,6 +201,7 @@ func runCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *
175
201
flags := cmd .Flags ()
176
202
flags .BoolVarP (& options .Detach , "detach" , "d" , false , "Run container in background and print container ID" )
177
203
flags .StringArrayVarP (& options .environment , "env" , "e" , []string {}, "Set environment variables" )
204
+ flags .StringArrayVar (& options .envFiles , "env-from-file" , []string {}, "Set environment variables from file" )
178
205
flags .StringArrayVarP (& options .labels , "label" , "l" , []string {}, "Add or override a label" )
179
206
flags .BoolVar (& options .Remove , "rm" , false , "Automatically remove the container when it exits" )
180
207
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
264
291
buildForRun = & bo
265
292
}
266
293
294
+ environment , err := options .getEnvironment ()
295
+ if err != nil {
296
+ return err
297
+ }
298
+
267
299
// start container and attach to container streams
268
300
runOpts := api.RunOptions {
269
301
Build : buildForRun ,
@@ -278,7 +310,7 @@ func runRun(ctx context.Context, backend api.Service, project *types.Project, op
278
310
User : options .user ,
279
311
CapAdd : options .capAdd .GetAll (),
280
312
CapDrop : options .capDrop .GetAll (),
281
- Environment : options . environment ,
313
+ Environment : environment . Values () ,
282
314
Entrypoint : options .entrypointCmd ,
283
315
Labels : labels ,
284
316
UseNetworkAliases : options .useAliases ,
0 commit comments