github.com/sijibomii/docker@v0.0.0-20231230191044-5cf6ca554647/api/client/exec.go (about) 1 package client 2 3 import ( 4 "fmt" 5 "io" 6 7 "golang.org/x/net/context" 8 9 "github.com/Sirupsen/logrus" 10 Cli "github.com/docker/docker/cli" 11 flag "github.com/docker/docker/pkg/mflag" 12 "github.com/docker/docker/pkg/promise" 13 "github.com/docker/engine-api/types" 14 ) 15 16 // CmdExec runs a command in a running container. 17 // 18 // Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...] 19 func (cli *DockerCli) CmdExec(args ...string) error { 20 cmd := Cli.Subcmd("exec", []string{"CONTAINER COMMAND [ARG...]"}, Cli.DockerCommands["exec"].Description, true) 21 detachKeys := cmd.String([]string{"-detach-keys"}, "", "Override the key sequence for detaching a container") 22 23 execConfig, err := ParseExec(cmd, args) 24 // just in case the ParseExec does not exit 25 if execConfig.Container == "" || err != nil { 26 return Cli.StatusError{StatusCode: 1} 27 } 28 29 if *detachKeys != "" { 30 cli.configFile.DetachKeys = *detachKeys 31 } 32 33 // Send client escape keys 34 execConfig.DetachKeys = cli.configFile.DetachKeys 35 36 response, err := cli.client.ContainerExecCreate(context.Background(), *execConfig) 37 if err != nil { 38 return err 39 } 40 41 execID := response.ID 42 if execID == "" { 43 fmt.Fprintf(cli.out, "exec ID empty") 44 return nil 45 } 46 47 //Temp struct for execStart so that we don't need to transfer all the execConfig 48 if !execConfig.Detach { 49 if err := cli.CheckTtyInput(execConfig.AttachStdin, execConfig.Tty); err != nil { 50 return err 51 } 52 } else { 53 execStartCheck := types.ExecStartCheck{ 54 Detach: execConfig.Detach, 55 Tty: execConfig.Tty, 56 } 57 58 if err := cli.client.ContainerExecStart(context.Background(), execID, execStartCheck); err != nil { 59 return err 60 } 61 // For now don't print this - wait for when we support exec wait() 62 // fmt.Fprintf(cli.out, "%s\n", execID) 63 return nil 64 } 65 66 // Interactive exec requested. 67 var ( 68 out, stderr io.Writer 69 in io.ReadCloser 70 errCh chan error 71 ) 72 73 if execConfig.AttachStdin { 74 in = cli.in 75 } 76 if execConfig.AttachStdout { 77 out = cli.out 78 } 79 if execConfig.AttachStderr { 80 if execConfig.Tty { 81 stderr = cli.out 82 } else { 83 stderr = cli.err 84 } 85 } 86 87 resp, err := cli.client.ContainerExecAttach(context.Background(), execID, *execConfig) 88 if err != nil { 89 return err 90 } 91 defer resp.Close() 92 errCh = promise.Go(func() error { 93 return cli.holdHijackedConnection(context.Background(), execConfig.Tty, in, out, stderr, resp) 94 }) 95 96 if execConfig.Tty && cli.isTerminalIn { 97 if err := cli.monitorTtySize(execID, true); err != nil { 98 fmt.Fprintf(cli.err, "Error monitoring TTY size: %s\n", err) 99 } 100 } 101 102 if err := <-errCh; err != nil { 103 logrus.Debugf("Error hijack: %s", err) 104 return err 105 } 106 107 var status int 108 if _, status, err = getExecExitCode(cli, execID); err != nil { 109 return err 110 } 111 112 if status != 0 { 113 return Cli.StatusError{StatusCode: status} 114 } 115 116 return nil 117 } 118 119 // ParseExec parses the specified args for the specified command and generates 120 // an ExecConfig from it. 121 // If the minimal number of specified args is not right or if specified args are 122 // not valid, it will return an error. 123 func ParseExec(cmd *flag.FlagSet, args []string) (*types.ExecConfig, error) { 124 var ( 125 flStdin = cmd.Bool([]string{"i", "-interactive"}, false, "Keep STDIN open even if not attached") 126 flTty = cmd.Bool([]string{"t", "-tty"}, false, "Allocate a pseudo-TTY") 127 flDetach = cmd.Bool([]string{"d", "-detach"}, false, "Detached mode: run command in the background") 128 flUser = cmd.String([]string{"u", "-user"}, "", "Username or UID (format: <name|uid>[:<group|gid>])") 129 flPrivileged = cmd.Bool([]string{"-privileged"}, false, "Give extended privileges to the command") 130 execCmd []string 131 container string 132 ) 133 cmd.Require(flag.Min, 2) 134 if err := cmd.ParseFlags(args, true); err != nil { 135 return nil, err 136 } 137 container = cmd.Arg(0) 138 parsedArgs := cmd.Args() 139 execCmd = parsedArgs[1:] 140 141 execConfig := &types.ExecConfig{ 142 User: *flUser, 143 Privileged: *flPrivileged, 144 Tty: *flTty, 145 Cmd: execCmd, 146 Container: container, 147 Detach: *flDetach, 148 } 149 150 // If -d is not set, attach to everything by default 151 if !*flDetach { 152 execConfig.AttachStdout = true 153 execConfig.AttachStderr = true 154 if *flStdin { 155 execConfig.AttachStdin = true 156 } 157 } 158 159 return execConfig, nil 160 }