diff --git a/daemon/mgr/container_exec.go b/daemon/mgr/container_exec.go index edf38de70..7d5997c25 100644 --- a/daemon/mgr/container_exec.go +++ b/daemon/mgr/container_exec.go @@ -27,7 +27,7 @@ func (mgr *ContainerManager) CreateExec(ctx context.Context, name string, config return "", fmt.Errorf("container %s is not running", c.ID) } - envs, err := mergeEnvSlice(c.Config.Env, config.Env) + envs, err := mergeEnvSlice(config.Env, c.Config.Env) if err != nil { return "", err diff --git a/test/cli_exec_test.go b/test/cli_exec_test.go index a6c9725e9..7fe6f9494 100644 --- a/test/cli_exec_test.go +++ b/test/cli_exec_test.go @@ -86,6 +86,27 @@ func (suite *PouchExecSuite) TestExecWithEnvs(c *check.C) { } } +// TestExecWithReplaceEnvs is to verify New Envs will replace old Envs. +func (suite *PouchExecSuite) TestExecWithReplaceEnvs(c *check.C) { + name := "exec-normal4" + res := command.PouchRun("run", "-d", "-e", "Test=Old", "--name", name, busyboxImage, "sleep", "100000") + defer DelContainerForceMultyTime(c, name) + + res.Assert(c, icmd.Success) + + res = command.PouchRun("exec", name, "env") + + if out := res.Combined(); !strings.Contains(out, "Test=Old") { + c.Fatalf("unexpected output %s expected %s\n", out, name) + } + + res = command.PouchRun("exec", "-e \"Test=New\"", name, "env") + + if out := res.Combined(); !strings.Contains(out, "Test=New") { + c.Fatalf("unexpected output %s expected %s\n", out, name) + } +} + // TestExecEcho tests exec prints the output. func (suite *PouchExecSuite) TestExecEcho(c *check.C) { name := "TestExecEcho"