Skip to content

Commit 7e01064

Browse files
committed
Add support to pass env-from-file to docker compose run
Signed-off-by: Vedant Koditkar <[email protected]> Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 55b5f23 commit 7e01064

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

cmd/compose/run.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ package compose
1919
import (
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,

pkg/e2e/compose_run_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,10 @@ func TestLocalComposeRun(t *testing.T) {
178178
assert.Assert(t, strings.Contains(res.Combined(), "backend Pulling"), res.Combined())
179179
assert.Assert(t, strings.Contains(res.Combined(), "backend Pulled"), res.Combined())
180180
})
181+
182+
t.Run("compose run --env-from-file", func(t *testing.T) {
183+
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/compose.yaml", "run", "--env-from-file", "./fixtures/run-test/run.env",
184+
"front", "env")
185+
res.Assert(t, icmd.Expected{Out: "FOO=BAR"})
186+
})
181187
}

pkg/e2e/fixtures/run-test/compose.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
version: '3.8'
21
services:
32
back:
43
image: alpine

pkg/e2e/fixtures/run-test/run.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FOO=BAR

0 commit comments

Comments
 (0)