container_exec.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package client // import "github.com/docker/docker/client"
  2. import (
  3. "context"
  4. "encoding/json"
  5. "net/http"
  6. "github.com/docker/docker/api/types"
  7. "github.com/docker/docker/api/types/versions"
  8. )
  9. // ContainerExecCreate creates a new exec configuration to run an exec process.
  10. func (cli *Client) ContainerExecCreate(ctx context.Context, container string, config types.ExecConfig) (types.IDResponse, error) {
  11. var response types.IDResponse
  12. // Make sure we negotiated (if the client is configured to do so),
  13. // as code below contains API-version specific handling of options.
  14. //
  15. // Normally, version-negotiation (if enabled) would not happen until
  16. // the API request is made.
  17. cli.checkVersion(ctx)
  18. if err := cli.NewVersionError(ctx, "1.25", "env"); len(config.Env) != 0 && err != nil {
  19. return response, err
  20. }
  21. if versions.LessThan(cli.ClientVersion(), "1.42") {
  22. config.ConsoleSize = nil
  23. }
  24. resp, err := cli.post(ctx, "/containers/"+container+"/exec", nil, config, nil)
  25. defer ensureReaderClosed(resp)
  26. if err != nil {
  27. return response, err
  28. }
  29. err = json.NewDecoder(resp.body).Decode(&response)
  30. return response, err
  31. }
  32. // ContainerExecStart starts an exec process already created in the docker host.
  33. func (cli *Client) ContainerExecStart(ctx context.Context, execID string, config types.ExecStartCheck) error {
  34. if versions.LessThan(cli.ClientVersion(), "1.42") {
  35. config.ConsoleSize = nil
  36. }
  37. resp, err := cli.post(ctx, "/exec/"+execID+"/start", nil, config, nil)
  38. ensureReaderClosed(resp)
  39. return err
  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. return cli.postHijacked(ctx, "/exec/"+execID+"/start", nil, config, http.Header{
  50. "Content-Type": {"application/json"},
  51. })
  52. }
  53. // ContainerExecInspect returns information about a specific exec process on the docker host.
  54. func (cli *Client) ContainerExecInspect(ctx context.Context, execID string) (types.ContainerExecInspect, error) {
  55. var response types.ContainerExecInspect
  56. resp, err := cli.get(ctx, "/exec/"+execID+"/json", nil, nil)
  57. if err != nil {
  58. return response, err
  59. }
  60. err = json.NewDecoder(resp.body).Decode(&response)
  61. ensureReaderClosed(resp)
  62. return response, err
  63. }