mirror of https://github.com/docker/cli.git
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:
parent
06ebd4517d
commit
a528b05dab
|
@ -11,7 +11,9 @@ import (
|
||||||
"github.com/docker/docker/cli"
|
"github.com/docker/docker/cli"
|
||||||
"github.com/docker/docker/cli/command"
|
"github.com/docker/docker/cli/command"
|
||||||
apiclient "github.com/docker/docker/client"
|
apiclient "github.com/docker/docker/client"
|
||||||
|
options "github.com/docker/docker/opts"
|
||||||
"github.com/docker/docker/pkg/promise"
|
"github.com/docker/docker/pkg/promise"
|
||||||
|
runconfigopts "github.com/docker/docker/runconfig/opts"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -22,11 +24,19 @@ type execOptions struct {
|
||||||
detach bool
|
detach bool
|
||||||
user string
|
user string
|
||||||
privileged bool
|
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`
|
// NewExecCommand creats a new cobra.Command for `docker exec`
|
||||||
func NewExecCommand(dockerCli *command.DockerCli) *cobra.Command {
|
func NewExecCommand(dockerCli *command.DockerCli) *cobra.Command {
|
||||||
var opts execOptions
|
opts := newExecOptions()
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "exec [OPTIONS] CONTAINER COMMAND [ARG...]",
|
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 {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
container := args[0]
|
container := args[0]
|
||||||
execCmd := args[1:]
|
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.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.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.BoolVarP(&opts.privileged, "privileged", "", false, "Give extended privileges to the command")
|
||||||
|
flags.VarP(opts.env, "env", "e", "Set environment variables")
|
||||||
|
|
||||||
return cmd
|
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
|
return execConfig, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue