From 4633f15f13d51530de2438c298a1084c55e4fedf Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Fri, 9 Sep 2016 16:50:54 -0700 Subject: [PATCH] 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 --- daemon/exec.go | 10 +++++++++ daemon/exec/exec.go | 1 + integration-cli/docker_cli_exec_unix_test.go | 23 ++++++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/daemon/exec.go b/daemon/exec.go index b535cf851b..e7352e81d8 100644 --- a/daemon/exec.go +++ b/daemon/exec.go @@ -17,7 +17,9 @@ import ( "github.com/docker/docker/libcontainerd" "github.com/docker/docker/pkg/pools" "github.com/docker/docker/pkg/signal" + "github.com/docker/docker/pkg/system" "github.com/docker/docker/pkg/term" + "github.com/docker/docker/utils" ) // Seconds to wait after sending TERM before trying KILL @@ -121,6 +123,13 @@ func (d *Daemon) ContainerExecCreate(name string, config *types.ExecConfig) (str execConfig.Tty = config.Tty execConfig.Privileged = config.Privileged execConfig.User = config.User + execConfig.Env = []string{ + "PATH=" + system.DefaultPathEnv, + } + if config.Tty { + execConfig.Env = append(execConfig.Env, "TERM=xterm") + } + execConfig.Env = utils.ReplaceOrAppendEnvValues(execConfig.Env, container.Config.Env) if len(execConfig.User) == 0 { execConfig.User = container.Config.User } @@ -195,6 +204,7 @@ func (d *Daemon) ContainerExecStart(ctx context.Context, name string, stdin io.R p := libcontainerd.Process{ Args: append([]string{ec.Entrypoint}, ec.Args...), + Env: ec.Env, Terminal: ec.Tty, } diff --git a/daemon/exec/exec.go b/daemon/exec/exec.go index bbeb1c16a6..5f4e7e73a1 100644 --- a/daemon/exec/exec.go +++ b/daemon/exec/exec.go @@ -27,6 +27,7 @@ type Config struct { Tty bool Privileged bool User string + Env []string } // NewConfig initializes the a new exec configuration diff --git a/integration-cli/docker_cli_exec_unix_test.go b/integration-cli/docker_cli_exec_unix_test.go index 42db4091dd..5f691196f1 100644 --- a/integration-cli/docker_cli_exec_unix_test.go +++ b/integration-cli/docker_cli_exec_unix_test.go @@ -68,3 +68,26 @@ func (s *DockerSuite) TestExecTTY(c *check.C) { c.Assert(err, checker.IsNil) c.Assert(bytes.Contains(buf, []byte("hello")), checker.Equals, true, check.Commentf(string(buf[:read]))) } + +// Test the the TERM env var is set when -t is provided on exec +func (s *DockerSuite) TestExecWithTERM(c *check.C) { + testRequires(c, DaemonIsLinux, SameHostDaemon) + out, _ := dockerCmd(c, "run", "-id", "busybox", "/bin/cat") + contID := strings.TrimSpace(out) + cmd := exec.Command(dockerBinary, "exec", "-t", contID, "sh", "-c", "if [ -z $TERM ]; then exit 1; else exit 0; fi") + if err := cmd.Run(); err != nil { + c.Assert(err, checker.IsNil) + } +} + +// Test that the TERM env var is not set on exec when -t is not provided, even if it was set +// on run +func (s *DockerSuite) TestExecWithNoTERM(c *check.C) { + testRequires(c, DaemonIsLinux, SameHostDaemon) + out, _ := dockerCmd(c, "run", "-itd", "busybox", "/bin/cat") + contID := strings.TrimSpace(out) + cmd := exec.Command(dockerBinary, "exec", contID, "sh", "-c", "if [ -z $TERM ]; then exit 0; else exit 1; fi") + if err := cmd.Run(); err != nil { + c.Assert(err, checker.IsNil) + } +}