github.com/tonistiigi/docker@v0.10.1-0.20240229224939-974013b0dc6a/client/container_exec.go (about) 1 package client // import "github.com/docker/docker/client" 2 3 import ( 4 "context" 5 "encoding/json" 6 "net/http" 7 8 "github.com/docker/docker/api/types" 9 "github.com/docker/docker/api/types/versions" 10 ) 11 12 // ContainerExecCreate creates a new exec configuration to run an exec process. 13 func (cli *Client) ContainerExecCreate(ctx context.Context, container string, config types.ExecConfig) (types.IDResponse, error) { 14 var response types.IDResponse 15 16 // Make sure we negotiated (if the client is configured to do so), 17 // as code below contains API-version specific handling of options. 18 // 19 // Normally, version-negotiation (if enabled) would not happen until 20 // the API request is made. 21 if err := cli.checkVersion(ctx); err != nil { 22 return response, err 23 } 24 25 if err := cli.NewVersionError(ctx, "1.25", "env"); len(config.Env) != 0 && err != nil { 26 return response, err 27 } 28 if versions.LessThan(cli.ClientVersion(), "1.42") { 29 config.ConsoleSize = nil 30 } 31 32 resp, err := cli.post(ctx, "/containers/"+container+"/exec", nil, config, nil) 33 defer ensureReaderClosed(resp) 34 if err != nil { 35 return response, err 36 } 37 err = json.NewDecoder(resp.body).Decode(&response) 38 return response, err 39 } 40 41 // ContainerExecStart starts an exec process already created in the docker host. 42 func (cli *Client) ContainerExecStart(ctx context.Context, execID string, config types.ExecStartCheck) error { 43 if versions.LessThan(cli.ClientVersion(), "1.42") { 44 config.ConsoleSize = nil 45 } 46 resp, err := cli.post(ctx, "/exec/"+execID+"/start", nil, config, nil) 47 ensureReaderClosed(resp) 48 return err 49 } 50 51 // ContainerExecAttach attaches a connection to an exec process in the server. 52 // It returns a types.HijackedConnection with the hijacked connection 53 // and the a reader to get output. It's up to the called to close 54 // the hijacked connection by calling types.HijackedResponse.Close. 55 func (cli *Client) ContainerExecAttach(ctx context.Context, execID string, config types.ExecStartCheck) (types.HijackedResponse, error) { 56 if versions.LessThan(cli.ClientVersion(), "1.42") { 57 config.ConsoleSize = nil 58 } 59 return cli.postHijacked(ctx, "/exec/"+execID+"/start", nil, config, http.Header{ 60 "Content-Type": {"application/json"}, 61 }) 62 } 63 64 // ContainerExecInspect returns information about a specific exec process on the docker host. 65 func (cli *Client) ContainerExecInspect(ctx context.Context, execID string) (types.ContainerExecInspect, error) { 66 var response types.ContainerExecInspect 67 resp, err := cli.get(ctx, "/exec/"+execID+"/json", nil, nil) 68 if err != nil { 69 return response, err 70 } 71 72 err = json.NewDecoder(resp.body).Decode(&response) 73 ensureReaderClosed(resp) 74 return response, err 75 }