Browse code

Add TERM env var to exec

When the `-t` flag is passed on exec make sure to add the TERM env var
to mirror the expected configuration from run.

Fixes #9299

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>

Michael Crosby authored on 2016/09/10 08:50:54
Showing 3 changed files
... ...
@@ -17,7 +17,9 @@ import (
17 17
 	"github.com/docker/docker/libcontainerd"
18 18
 	"github.com/docker/docker/pkg/pools"
19 19
 	"github.com/docker/docker/pkg/signal"
20
+	"github.com/docker/docker/pkg/system"
20 21
 	"github.com/docker/docker/pkg/term"
22
+	"github.com/docker/docker/utils"
21 23
 )
22 24
 
23 25
 // Seconds to wait after sending TERM before trying KILL
... ...
@@ -121,6 +123,13 @@ func (d *Daemon) ContainerExecCreate(name string, config *types.ExecConfig) (str
121 121
 	execConfig.Tty = config.Tty
122 122
 	execConfig.Privileged = config.Privileged
123 123
 	execConfig.User = config.User
124
+	execConfig.Env = []string{
125
+		"PATH=" + system.DefaultPathEnv,
126
+	}
127
+	if config.Tty {
128
+		execConfig.Env = append(execConfig.Env, "TERM=xterm")
129
+	}
130
+	execConfig.Env = utils.ReplaceOrAppendEnvValues(execConfig.Env, container.Config.Env)
124 131
 	if len(execConfig.User) == 0 {
125 132
 		execConfig.User = container.Config.User
126 133
 	}
... ...
@@ -195,6 +204,7 @@ func (d *Daemon) ContainerExecStart(ctx context.Context, name string, stdin io.R
195 195
 
196 196
 	p := libcontainerd.Process{
197 197
 		Args:     append([]string{ec.Entrypoint}, ec.Args...),
198
+		Env:      ec.Env,
198 199
 		Terminal: ec.Tty,
199 200
 	}
200 201
 
... ...
@@ -27,6 +27,7 @@ type Config struct {
27 27
 	Tty         bool
28 28
 	Privileged  bool
29 29
 	User        string
30
+	Env         []string
30 31
 }
31 32
 
32 33
 // NewConfig initializes the a new exec configuration
... ...
@@ -68,3 +68,26 @@ func (s *DockerSuite) TestExecTTY(c *check.C) {
68 68
 	c.Assert(err, checker.IsNil)
69 69
 	c.Assert(bytes.Contains(buf, []byte("hello")), checker.Equals, true, check.Commentf(string(buf[:read])))
70 70
 }
71
+
72
+// Test the the TERM env var is set when -t is provided on exec
73
+func (s *DockerSuite) TestExecWithTERM(c *check.C) {
74
+	testRequires(c, DaemonIsLinux, SameHostDaemon)
75
+	out, _ := dockerCmd(c, "run", "-id", "busybox", "/bin/cat")
76
+	contID := strings.TrimSpace(out)
77
+	cmd := exec.Command(dockerBinary, "exec", "-t", contID, "sh", "-c", "if [ -z $TERM ]; then exit 1; else exit 0; fi")
78
+	if err := cmd.Run(); err != nil {
79
+		c.Assert(err, checker.IsNil)
80
+	}
81
+}
82
+
83
+// Test that the TERM env var is not set on exec when -t is not provided, even if it was set
84
+// on run
85
+func (s *DockerSuite) TestExecWithNoTERM(c *check.C) {
86
+	testRequires(c, DaemonIsLinux, SameHostDaemon)
87
+	out, _ := dockerCmd(c, "run", "-itd", "busybox", "/bin/cat")
88
+	contID := strings.TrimSpace(out)
89
+	cmd := exec.Command(dockerBinary, "exec", contID, "sh", "-c", "if [ -z $TERM ]; then exit 0; else exit 1; fi")
90
+	if err := cmd.Run(); err != nil {
91
+		c.Assert(err, checker.IsNil)
92
+	}
93
+}