container_exec.go 2.0 KB

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