container_exec.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. if err := cli.NewVersionError("1.25", "env"); len(config.Env) != 0 && err != nil {
  13. return response, err
  14. }
  15. if versions.LessThan(cli.ClientVersion(), "1.42") {
  16. config.ConsoleSize = nil
  17. }
  18. resp, err := cli.post(ctx, "/containers/"+container+"/exec", nil, config, nil)
  19. defer ensureReaderClosed(resp)
  20. if err != nil {
  21. return response, err
  22. }
  23. err = json.NewDecoder(resp.body).Decode(&response)
  24. return response, err
  25. }
  26. // ContainerExecStart starts an exec process already created in the docker host.
  27. func (cli *Client) ContainerExecStart(ctx context.Context, execID string, config types.ExecStartCheck) error {
  28. if versions.LessThan(cli.ClientVersion(), "1.42") {
  29. config.ConsoleSize = nil
  30. }
  31. resp, err := cli.post(ctx, "/exec/"+execID+"/start", nil, config, nil)
  32. ensureReaderClosed(resp)
  33. return err
  34. }
  35. // ContainerExecAttach attaches a connection to an exec process in the server.
  36. // It returns a types.HijackedConnection with the hijacked connection
  37. // and the a reader to get output. It's up to the called to close
  38. // the hijacked connection by calling types.HijackedResponse.Close.
  39. func (cli *Client) ContainerExecAttach(ctx context.Context, execID string, config types.ExecStartCheck) (types.HijackedResponse, error) {
  40. if versions.LessThan(cli.ClientVersion(), "1.42") {
  41. config.ConsoleSize = nil
  42. }
  43. return cli.postHijacked(ctx, "/exec/"+execID+"/start", nil, config, http.Header{
  44. "Content-Type": {"application/json"},
  45. })
  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. }