container_exec.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package client
  2. import (
  3. "encoding/json"
  4. "github.com/docker/engine-api/types"
  5. "golang.org/x/net/context"
  6. )
  7. // ContainerExecCreate creates a new exec configuration to run an exec process.
  8. func (cli *Client) ContainerExecCreate(ctx context.Context, config types.ExecConfig) (types.ContainerExecCreateResponse, error) {
  9. var response types.ContainerExecCreateResponse
  10. resp, err := cli.post(ctx, "/containers/"+config.Container+"/exec", nil, config, nil)
  11. if err != nil {
  12. return response, err
  13. }
  14. err = json.NewDecoder(resp.body).Decode(&response)
  15. ensureReaderClosed(resp)
  16. return response, err
  17. }
  18. // ContainerExecStart starts an exec process already create in the docker host.
  19. func (cli *Client) ContainerExecStart(ctx context.Context, execID string, config types.ExecStartCheck) error {
  20. resp, err := cli.post(ctx, "/exec/"+execID+"/start", nil, config, nil)
  21. ensureReaderClosed(resp)
  22. return err
  23. }
  24. // ContainerExecAttach attaches a connection to an exec process in the server.
  25. // It returns a types.HijackedConnection with the hijacked connection
  26. // and the a reader to get output. It's up to the called to close
  27. // the hijacked connection by calling types.HijackedResponse.Close.
  28. func (cli *Client) ContainerExecAttach(ctx context.Context, execID string, config types.ExecConfig) (types.HijackedResponse, error) {
  29. headers := map[string][]string{"Content-Type": {"application/json"}}
  30. return cli.postHijacked(ctx, "/exec/"+execID+"/start", nil, config, headers)
  31. }
  32. // ContainerExecInspect returns information about a specific exec process on the docker host.
  33. func (cli *Client) ContainerExecInspect(ctx context.Context, execID string) (types.ContainerExecInspect, error) {
  34. var response types.ContainerExecInspect
  35. resp, err := cli.get(ctx, "/exec/"+execID+"/json", nil, nil)
  36. if err != nil {
  37. return response, err
  38. }
  39. err = json.NewDecoder(resp.body).Decode(&response)
  40. ensureReaderClosed(resp)
  41. return response, err
  42. }