github.com/adityamillind98/moby@v23.0.0-rc.4+incompatible/client/container_exec.go (about) 1 package client // import "github.com/docker/docker/client" 2 3 import ( 4 "context" 5 "encoding/json" 6 7 "github.com/docker/docker/api/types" 8 "github.com/docker/docker/api/types/versions" 9 ) 10 11 // ContainerExecCreate creates a new exec configuration to run an exec process. 12 func (cli *Client) ContainerExecCreate(ctx context.Context, container string, config types.ExecConfig) (types.IDResponse, error) { 13 var response types.IDResponse 14 15 if err := cli.NewVersionError("1.25", "env"); len(config.Env) != 0 && err != nil { 16 return response, err 17 } 18 if versions.LessThan(cli.ClientVersion(), "1.42") { 19 config.ConsoleSize = nil 20 } 21 22 resp, err := cli.post(ctx, "/containers/"+container+"/exec", nil, config, nil) 23 defer ensureReaderClosed(resp) 24 if err != nil { 25 return response, err 26 } 27 err = json.NewDecoder(resp.body).Decode(&response) 28 return response, err 29 } 30 31 // ContainerExecStart starts an exec process already created in the docker host. 32 func (cli *Client) ContainerExecStart(ctx context.Context, execID string, config types.ExecStartCheck) error { 33 if versions.LessThan(cli.ClientVersion(), "1.42") { 34 config.ConsoleSize = nil 35 } 36 resp, err := cli.post(ctx, "/exec/"+execID+"/start", nil, config, nil) 37 ensureReaderClosed(resp) 38 return err 39 } 40 41 // ContainerExecAttach attaches a connection to an exec process in the server. 42 // It returns a types.HijackedConnection with the hijacked connection 43 // and the a reader to get output. It's up to the called to close 44 // the hijacked connection by calling types.HijackedResponse.Close. 45 func (cli *Client) ContainerExecAttach(ctx context.Context, execID string, config types.ExecStartCheck) (types.HijackedResponse, error) { 46 if versions.LessThan(cli.ClientVersion(), "1.42") { 47 config.ConsoleSize = nil 48 } 49 headers := map[string][]string{ 50 "Content-Type": {"application/json"}, 51 } 52 return cli.postHijacked(ctx, "/exec/"+execID+"/start", nil, config, headers) 53 } 54 55 // ContainerExecInspect returns information about a specific exec process on the docker host. 56 func (cli *Client) ContainerExecInspect(ctx context.Context, execID string) (types.ContainerExecInspect, error) { 57 var response types.ContainerExecInspect 58 resp, err := cli.get(ctx, "/exec/"+execID+"/json", nil, nil) 59 if err != nil { 60 return response, err 61 } 62 63 err = json.NewDecoder(resp.body).Decode(&response) 64 ensureReaderClosed(resp) 65 return response, err 66 }