container_exec.go 2.3 KB

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