RunExec executes an `exec` command
(ctx context.Context, dockerCLI command.Cli, containerIDorName string, options ExecOptions)
| 86 | |
| 87 | // RunExec executes an `exec` command |
| 88 | func RunExec(ctx context.Context, dockerCLI command.Cli, containerIDorName string, options ExecOptions) error { |
| 89 | execOptions, err := parseExec(options, dockerCLI.ConfigFile()) |
| 90 | if err != nil { |
| 91 | return err |
| 92 | } |
| 93 | |
| 94 | apiClient := dockerCLI.Client() |
| 95 | |
| 96 | // We need to check the tty _before_ we do the ContainerExecCreate, because |
| 97 | // otherwise if we error out we will leak execIDs on the server (and |
| 98 | // there's no easy way to clean those up). But also in order to make "not |
| 99 | // exist" errors take precedence we do a dummy inspect first. |
| 100 | if _, err := apiClient.ContainerInspect(ctx, containerIDorName, client.ContainerInspectOptions{}); err != nil { |
| 101 | return err |
| 102 | } |
| 103 | if !options.Detach { |
| 104 | if err := dockerCLI.In().CheckTty(execOptions.AttachStdin, execOptions.TTY); err != nil { |
| 105 | return err |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | fillConsoleSize(execOptions, dockerCLI) |
| 110 | |
| 111 | response, err := apiClient.ExecCreate(ctx, containerIDorName, *execOptions) |
| 112 | if err != nil { |
| 113 | return err |
| 114 | } |
| 115 | |
| 116 | execID := response.ID |
| 117 | if execID == "" { |
| 118 | return errors.New("exec ID empty") |
| 119 | } |
| 120 | |
| 121 | if options.Detach { |
| 122 | _, err := apiClient.ExecStart(ctx, execID, client.ExecStartOptions{ |
| 123 | Detach: options.Detach, |
| 124 | TTY: execOptions.TTY, |
| 125 | ConsoleSize: client.ConsoleSize{Height: execOptions.ConsoleSize.Height, Width: execOptions.ConsoleSize.Width}, |
| 126 | }) |
| 127 | return err |
| 128 | } |
| 129 | return interactiveExec(ctx, dockerCLI, execOptions, execID) |
| 130 | } |
| 131 | |
| 132 | func fillConsoleSize(execOptions *client.ExecCreateOptions, dockerCli command.Cli) { |
| 133 | if execOptions.TTY { |
searching dependent graphs…