exec.go 1.8 KB

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