exec.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package lib
  2. import (
  3. "encoding/json"
  4. "github.com/docker/docker/api/types"
  5. "github.com/docker/docker/runconfig"
  6. )
  7. // ContainerExecCreate creates a new exec configuration to run an exec process.
  8. func (cli *Client) ContainerExecCreate(config runconfig.ExecConfig) (types.ContainerExecCreateResponse, error) {
  9. var response types.ContainerExecCreateResponse
  10. resp, err := cli.post("/containers/"+config.Container+"/exec", nil, config, nil)
  11. if err != nil {
  12. return response, err
  13. }
  14. defer ensureReaderClosed(resp)
  15. err = json.NewDecoder(resp.body).Decode(&response)
  16. return response, err
  17. }
  18. // ContainerExecStart starts an exec process already create in the docker host.
  19. func (cli *Client) ContainerExecStart(execID string, config types.ExecStartCheck) error {
  20. resp, err := cli.post("/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(execID string, config runconfig.ExecConfig) (types.HijackedResponse, error) {
  29. headers := map[string][]string{"Content-Type": {"application/json"}}
  30. return cli.postHijacked("/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(execID string) (types.ContainerExecInspect, error) {
  34. var response types.ContainerExecInspect
  35. resp, err := cli.get("/exec/"+execID+"/json", nil, nil)
  36. if err != nil {
  37. return response, err
  38. }
  39. defer ensureReaderClosed(resp)
  40. err = json.NewDecoder(resp.body).Decode(&response)
  41. return response, err
  42. }