2016-09-08 13:11:39 -04:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2016-09-08 13:11:39 -04:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2022-03-30 09:27:25 -04:00
|
|
|
"os"
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2022-03-30 09:27:25 -04:00
|
|
|
"github.com/docker/cli/cli/command/completion"
|
2017-05-09 18:35:25 -04:00
|
|
|
"github.com/docker/cli/cli/config/configfile"
|
2017-05-15 08:45:19 -04:00
|
|
|
"github.com/docker/cli/opts"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2017-05-08 13:51:30 -04:00
|
|
|
apiclient "github.com/docker/docker/client"
|
2017-05-09 18:35:25 -04:00
|
|
|
"github.com/pkg/errors"
|
2017-08-07 05:52:40 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2022-02-22 04:36:43 -05:00
|
|
|
// ExecOptions group options for `exec` command
|
|
|
|
type ExecOptions struct {
|
|
|
|
DetachKeys string
|
|
|
|
Interactive bool
|
|
|
|
TTY bool
|
|
|
|
Detach bool
|
|
|
|
User string
|
|
|
|
Privileged bool
|
|
|
|
Env opts.ListOpts
|
|
|
|
Workdir string
|
|
|
|
Container string
|
|
|
|
Command []string
|
|
|
|
EnvFile opts.ListOpts
|
2016-07-13 13:24:41 -04:00
|
|
|
}
|
|
|
|
|
2022-02-22 04:36:43 -05:00
|
|
|
// NewExecOptions creates a new ExecOptions
|
|
|
|
func NewExecOptions() ExecOptions {
|
|
|
|
return ExecOptions{
|
|
|
|
Env: opts.NewListOpts(opts.ValidateEnv),
|
|
|
|
EnvFile: opts.NewListOpts(nil),
|
2020-06-29 18:32:44 -04:00
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2017-02-16 10:56:53 -05:00
|
|
|
// NewExecCommand creates a new cobra.Command for `docker exec`
|
2017-06-07 19:03:52 -04:00
|
|
|
func NewExecCommand(dockerCli command.Cli) *cobra.Command {
|
2022-02-22 04:36:43 -05:00
|
|
|
options := NewExecOptions()
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "exec [OPTIONS] CONTAINER COMMAND [ARG...]",
|
2022-03-29 17:54:35 -04:00
|
|
|
Short: "Execute a command in a running container",
|
2016-09-08 13:11:39 -04:00
|
|
|
Args: cli.RequiresMinArgs(2),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2022-02-22 04:36:43 -05:00
|
|
|
options.Container = args[0]
|
|
|
|
options.Command = args[1:]
|
|
|
|
return RunExec(dockerCli, options)
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
2022-05-12 07:18:48 -04:00
|
|
|
ValidArgsFunction: completion.ContainerNames(dockerCli, false, func(container types.Container) bool {
|
|
|
|
return container.State != "paused"
|
|
|
|
}),
|
2022-03-30 03:37:08 -04:00
|
|
|
Annotations: map[string]string{
|
|
|
|
"category-top": "2",
|
|
|
|
},
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.SetInterspersed(false)
|
|
|
|
|
2022-02-22 04:36:43 -05:00
|
|
|
flags.StringVarP(&options.DetachKeys, "detach-keys", "", "", "Override the key sequence for detaching a container")
|
|
|
|
flags.BoolVarP(&options.Interactive, "interactive", "i", false, "Keep STDIN open even if not attached")
|
|
|
|
flags.BoolVarP(&options.TTY, "tty", "t", false, "Allocate a pseudo-TTY")
|
|
|
|
flags.BoolVarP(&options.Detach, "detach", "d", false, "Detached mode: run command in the background")
|
|
|
|
flags.StringVarP(&options.User, "user", "u", "", "Username or UID (format: <name|uid>[:<group|gid>])")
|
|
|
|
flags.BoolVarP(&options.Privileged, "privileged", "", false, "Give extended privileges to the command")
|
|
|
|
flags.VarP(&options.Env, "env", "e", "Set environment variables")
|
2016-11-02 20:43:32 -04:00
|
|
|
flags.SetAnnotation("env", "version", []string{"1.25"})
|
2022-02-22 04:36:43 -05:00
|
|
|
flags.Var(&options.EnvFile, "env-file", "Read in a file of environment variables")
|
2020-06-29 18:32:44 -04:00
|
|
|
flags.SetAnnotation("env-file", "version", []string{"1.25"})
|
2022-02-22 04:36:43 -05:00
|
|
|
flags.StringVarP(&options.Workdir, "workdir", "w", "", "Working directory inside the container")
|
2017-12-06 04:33:36 -05:00
|
|
|
flags.SetAnnotation("workdir", "version", []string{"1.35"})
|
2022-03-30 09:27:25 -04:00
|
|
|
|
|
|
|
cmd.RegisterFlagCompletionFunc(
|
|
|
|
"env",
|
|
|
|
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
|
|
return os.Environ(), cobra.ShellCompDirectiveNoFileComp
|
|
|
|
},
|
|
|
|
)
|
|
|
|
cmd.RegisterFlagCompletionFunc(
|
|
|
|
"env-file",
|
|
|
|
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
|
|
return nil, cobra.ShellCompDirectiveDefault // _filedir
|
|
|
|
},
|
|
|
|
)
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2022-02-22 04:36:43 -05:00
|
|
|
// RunExec executes an `exec` command
|
|
|
|
func RunExec(dockerCli command.Cli, options ExecOptions) error {
|
2020-06-29 18:32:44 -04:00
|
|
|
execConfig, err := parseExec(options, dockerCli.ConfigFile())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
client := dockerCli.Client()
|
|
|
|
|
2017-05-09 07:07:40 -04:00
|
|
|
// We need to check the tty _before_ we do the ContainerExecCreate, because
|
|
|
|
// otherwise if we error out we will leak execIDs on the server (and
|
|
|
|
// there's no easy way to clean those up). But also in order to make "not
|
|
|
|
// exist" errors take precedence we do a dummy inspect first.
|
2022-02-22 04:36:43 -05:00
|
|
|
if _, err := client.ContainerInspect(ctx, options.Container); err != nil {
|
2017-05-09 07:07:40 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !execConfig.Detach {
|
|
|
|
if err := dockerCli.In().CheckTty(execConfig.AttachStdin, execConfig.Tty); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 04:36:43 -05:00
|
|
|
response, err := client.ContainerExecCreate(ctx, options.Container, *execConfig)
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
execID := response.ID
|
|
|
|
if execID == "" {
|
2017-05-09 18:35:25 -04:00
|
|
|
return errors.New("exec ID empty")
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2017-05-09 07:07:40 -04:00
|
|
|
if execConfig.Detach {
|
2016-09-08 13:11:39 -04:00
|
|
|
execStartCheck := types.ExecStartCheck{
|
|
|
|
Detach: execConfig.Detach,
|
|
|
|
Tty: execConfig.Tty,
|
|
|
|
}
|
2017-06-09 17:42:16 -04:00
|
|
|
return client.ContainerExecStart(ctx, execID, execStartCheck)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2017-05-09 18:35:25 -04:00
|
|
|
return interactiveExec(ctx, dockerCli, execConfig, execID)
|
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2017-05-09 18:35:25 -04:00
|
|
|
func interactiveExec(ctx context.Context, dockerCli command.Cli, execConfig *types.ExecConfig, execID string) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
// Interactive exec requested.
|
|
|
|
var (
|
|
|
|
out, stderr io.Writer
|
|
|
|
in io.ReadCloser
|
|
|
|
)
|
|
|
|
|
|
|
|
if execConfig.AttachStdin {
|
|
|
|
in = dockerCli.In()
|
|
|
|
}
|
|
|
|
if execConfig.AttachStdout {
|
|
|
|
out = dockerCli.Out()
|
|
|
|
}
|
|
|
|
if execConfig.AttachStderr {
|
|
|
|
if execConfig.Tty {
|
|
|
|
stderr = dockerCli.Out()
|
|
|
|
} else {
|
|
|
|
stderr = dockerCli.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-09 18:35:25 -04:00
|
|
|
client := dockerCli.Client()
|
2017-10-11 09:31:09 -04:00
|
|
|
execStartCheck := types.ExecStartCheck{
|
|
|
|
Tty: execConfig.Tty,
|
|
|
|
}
|
|
|
|
resp, err := client.ContainerExecAttach(ctx, execID, execStartCheck)
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Close()
|
2017-05-17 14:40:56 -04:00
|
|
|
|
2017-09-29 08:18:19 -04:00
|
|
|
errCh := make(chan error, 1)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(errCh)
|
|
|
|
errCh <- func() error {
|
|
|
|
streamer := hijackedIOStreamer{
|
|
|
|
streams: dockerCli,
|
|
|
|
inputStream: in,
|
|
|
|
outputStream: out,
|
|
|
|
errorStream: stderr,
|
|
|
|
resp: resp,
|
|
|
|
tty: execConfig.Tty,
|
|
|
|
detachKeys: execConfig.DetachKeys,
|
|
|
|
}
|
|
|
|
|
|
|
|
return streamer.stream(ctx)
|
|
|
|
}()
|
|
|
|
}()
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
if execConfig.Tty && dockerCli.In().IsTerminal() {
|
|
|
|
if err := MonitorTtySize(ctx, dockerCli, execID, true); err != nil {
|
2016-12-24 19:05:37 -05:00
|
|
|
fmt.Fprintln(dockerCli.Err(), "Error monitoring TTY size:", err)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := <-errCh; err != nil {
|
|
|
|
logrus.Debugf("Error hijack: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-09 18:35:25 -04:00
|
|
|
return getExecExitStatus(ctx, client, execID)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2017-05-09 18:35:25 -04:00
|
|
|
func getExecExitStatus(ctx context.Context, client apiclient.ContainerAPIClient, execID string) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
resp, err := client.ContainerExecInspect(ctx, execID)
|
|
|
|
if err != nil {
|
|
|
|
// If we can't connect, then the daemon probably died.
|
2016-11-21 21:42:55 -05:00
|
|
|
if !apiclient.IsErrConnectionFailed(err) {
|
2017-05-09 18:35:25 -04:00
|
|
|
return err
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2017-05-09 18:35:25 -04:00
|
|
|
return cli.StatusError{StatusCode: -1}
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2017-05-09 18:35:25 -04:00
|
|
|
status := resp.ExitCode
|
|
|
|
if status != 0 {
|
|
|
|
return cli.StatusError{StatusCode: status}
|
|
|
|
}
|
|
|
|
return nil
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// parseExec parses the specified args for the specified command and generates
|
|
|
|
// an ExecConfig from it.
|
2022-02-22 04:36:43 -05:00
|
|
|
func parseExec(execOpts ExecOptions, configFile *configfile.ConfigFile) (*types.ExecConfig, error) {
|
2016-09-08 13:11:39 -04:00
|
|
|
execConfig := &types.ExecConfig{
|
2022-02-22 04:36:43 -05:00
|
|
|
User: execOpts.User,
|
|
|
|
Privileged: execOpts.Privileged,
|
|
|
|
Tty: execOpts.TTY,
|
|
|
|
Cmd: execOpts.Command,
|
|
|
|
Detach: execOpts.Detach,
|
|
|
|
WorkingDir: execOpts.Workdir,
|
2020-06-29 18:32:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// collect all the environment variables for the container
|
|
|
|
var err error
|
2022-02-22 04:36:43 -05:00
|
|
|
if execConfig.Env, err = opts.ReadKVEnvStrings(execOpts.EnvFile.GetAll(), execOpts.Env.GetAll()); err != nil {
|
2020-06-29 18:32:44 -04:00
|
|
|
return nil, err
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// If -d is not set, attach to everything by default
|
2022-02-22 04:36:43 -05:00
|
|
|
if !execOpts.Detach {
|
2016-09-08 13:11:39 -04:00
|
|
|
execConfig.AttachStdout = true
|
|
|
|
execConfig.AttachStderr = true
|
2022-02-22 04:36:43 -05:00
|
|
|
if execOpts.Interactive {
|
2016-09-08 13:11:39 -04:00
|
|
|
execConfig.AttachStdin = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 04:36:43 -05:00
|
|
|
if execOpts.DetachKeys != "" {
|
|
|
|
execConfig.DetachKeys = execOpts.DetachKeys
|
2017-05-09 18:35:25 -04:00
|
|
|
} else {
|
|
|
|
execConfig.DetachKeys = configFile.DetachKeys
|
2016-07-13 13:24:41 -04:00
|
|
|
}
|
2020-06-29 18:32:44 -04:00
|
|
|
return execConfig, nil
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|