Skip to content

Commit 10cfd55

Browse files
authored
Merge pull request docker#9761 from ulyssessouza/refactor-osenv-precedence
Give environment variables precedence back to OS over .env
2 parents a64a5a6 + 3f4f4e5 commit 10cfd55

File tree

4 files changed

+11
-9
lines changed

4 files changed

+11
-9
lines changed

cmd/compose/compose.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,10 @@ func setEnvWithDotEnv(prjOpts *projectOptions) error {
383383
return err
384384
}
385385
for k, v := range envFromFile {
386-
if err := os.Setenv(k, v); err != nil { // overwrite the process env with merged OS + env file results
387-
return err
386+
if _, ok := os.LookupEnv(k); !ok { // Precedence to OS Env
387+
if err := os.Setenv(k, v); err != nil {
388+
return err
389+
}
388390
}
389391
}
390392
return nil

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/AlecAivazis/survey/v2 v2.3.5
77
github.com/buger/goterm v1.0.4
88
github.com/cnabio/cnab-to-oci v0.3.6
9-
github.com/compose-spec/compose-go v1.4.0
9+
github.com/compose-spec/compose-go v1.5.0
1010
github.com/containerd/console v1.0.3
1111
github.com/containerd/containerd v1.6.8
1212
github.com/distribution/distribution/v3 v3.0.0-20220729163034-26163d82560f

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,8 @@ github.com/cockroachdb/errors v1.2.4/go.mod h1:rQD95gz6FARkaKkQXUksEje/d9a6wBJoC
286286
github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f/go.mod h1:i/u985jwjWRlyHXQbwatDASoW0RMlZ/3i9yJHE2xLkI=
287287
github.com/codahale/hdrhistogram v0.0.0-20160425231609-f8ad88b59a58/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI=
288288
github.com/compose-spec/compose-go v1.2.1/go.mod h1:pAy7Mikpeft4pxkFU565/DRHEbDfR84G6AQuiL+Hdg8=
289-
github.com/compose-spec/compose-go v1.4.0 h1:zaYVAZ6lIByr7Jffi20AabfeUwcTrdXfH3X1R5HEm+g=
290-
github.com/compose-spec/compose-go v1.4.0/go.mod h1:l7RUULbFFLzlQHuxtJr7SVLyWdqEpbJEGTWCgcu6Eqw=
289+
github.com/compose-spec/compose-go v1.5.0 h1:yOmYpIm13pYt2o+oKVe/JAD6o2Tv+eUyOcRhf0qF4fA=
290+
github.com/compose-spec/compose-go v1.5.0/go.mod h1:l7RUULbFFLzlQHuxtJr7SVLyWdqEpbJEGTWCgcu6Eqw=
291291
github.com/containerd/aufs v0.0.0-20200908144142-dab0cbea06f4/go.mod h1:nukgQABAEopAHvB6j7cnP5zJ+/3aVcE7hCYqvIwAHyE=
292292
github.com/containerd/aufs v0.0.0-20201003224125-76a6863f2989/go.mod h1:AkGGQs9NM2vtYHaUen+NljV0/baGCAPELGm2q9ZXpWU=
293293
github.com/containerd/aufs v0.0.0-20210316121734-20793ff83c97/go.mod h1:kL5kd6KM5TzQjR79jljyi4olc1Vrx6XBlcyj3gNv2PU=

pkg/e2e/compose_environment_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestEnvPriority(t *testing.T) {
3434
})
3535

3636
// Full options activated
37-
// 1. Command Line (docker compose run --env <KEY[=VAL]>) <-- Result expected (From environment patched by --env-file)
37+
// 1. Command Line (docker compose run --env <KEY[=VAL]>) <-- Result expected (From OS Environment)
3838
// 2. Compose File (service::environment section)
3939
// 3. Compose File (service::env_file section file)
4040
// 4. Container Image ENV directive
@@ -45,7 +45,7 @@ func TestEnvPriority(t *testing.T) {
4545
"run", "--rm", "-e", "WHEREAMI", "env-compose-priority")
4646
cmd.Env = append(cmd.Env, "WHEREAMI=shell")
4747
res := icmd.RunCmd(cmd)
48-
assert.Equal(t, strings.TrimSpace(res.Stdout()), "override")
48+
assert.Equal(t, strings.TrimSpace(res.Stdout()), "shell")
4949
})
5050

5151
// Full options activated
@@ -63,7 +63,7 @@ func TestEnvPriority(t *testing.T) {
6363
})
6464

6565
// No Compose file, all other options
66-
// 1. Command Line (docker compose run --env <KEY[=VAL]>) <-- Result expected (From environment patched by --env-file)
66+
// 1. Command Line (docker compose run --env <KEY[=VAL]>) <-- Result expected (From OS Environment)
6767
// 2. Compose File (service::environment section)
6868
// 3. Compose File (service::env_file section file)
6969
// 4. Container Image ENV directive
@@ -74,7 +74,7 @@ func TestEnvPriority(t *testing.T) {
7474
"run", "--rm", "-e", "WHEREAMI", "env-compose-priority")
7575
cmd.Env = append(cmd.Env, "WHEREAMI=shell")
7676
res := icmd.RunCmd(cmd)
77-
assert.Equal(t, strings.TrimSpace(res.Stdout()), "override")
77+
assert.Equal(t, strings.TrimSpace(res.Stdout()), "shell")
7878
})
7979

8080
// No Compose file, all other options with env variable from OS environment

0 commit comments

Comments
 (0)