Exec: Add ability to set environment variables

Keeping the current behavior for exec, i.e., inheriting
variables from main process. New variables will be added
to current ones. If there's already a variable with that
name it will be overwritten.

Example of usage: docker exec -it -e TERM=vt100 <container> top

Closes #24355.

Signed-off-by: Jonh Wendell <jonh.wendell@redhat.com>
This commit is contained in:
Jonh Wendell 2016-07-13 14:24:41 -03:00
parent 06ebd4517d
commit a528b05dab
1 changed files with 17 additions and 2 deletions

View File

@ -11,7 +11,9 @@ import (
"github.com/docker/docker/cli"
"github.com/docker/docker/cli/command"
apiclient "github.com/docker/docker/client"
options "github.com/docker/docker/opts"
"github.com/docker/docker/pkg/promise"
runconfigopts "github.com/docker/docker/runconfig/opts"
"github.com/spf13/cobra"
)
@ -22,11 +24,19 @@ type execOptions struct {
detach bool
user string
privileged bool
env *options.ListOpts
}
func newExecOptions() *execOptions {
var values []string
return &execOptions{
env: options.NewListOptsRef(&values, runconfigopts.ValidateEnv),
}
}
// NewExecCommand creats a new cobra.Command for `docker exec`
func NewExecCommand(dockerCli *command.DockerCli) *cobra.Command {
var opts execOptions
opts := newExecOptions()
cmd := &cobra.Command{
Use: "exec [OPTIONS] CONTAINER COMMAND [ARG...]",
@ -35,7 +45,7 @@ func NewExecCommand(dockerCli *command.DockerCli) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
container := args[0]
execCmd := args[1:]
return runExec(dockerCli, &opts, container, execCmd)
return runExec(dockerCli, opts, container, execCmd)
},
}
@ -48,6 +58,7 @@ func NewExecCommand(dockerCli *command.DockerCli) *cobra.Command {
flags.BoolVarP(&opts.detach, "detach", "d", false, "Detached mode: run command in the background")
flags.StringVarP(&opts.user, "user", "u", "", "Username or UID (format: <name|uid>[:<group|gid>])")
flags.BoolVarP(&opts.privileged, "privileged", "", false, "Give extended privileges to the command")
flags.VarP(opts.env, "env", "e", "Set environment variables")
return cmd
}
@ -188,5 +199,9 @@ func parseExec(opts *execOptions, container string, execCmd []string) (*types.Ex
}
}
if opts.env != nil {
execConfig.Env = opts.env.GetAll()
}
return execConfig, nil
}