container_inspect.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package client
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io/ioutil"
  6. "net/http"
  7. "net/url"
  8. "github.com/docker/engine-api/types"
  9. "golang.org/x/net/context"
  10. )
  11. // ContainerInspect returns the container information.
  12. func (cli *Client) ContainerInspect(ctx context.Context, containerID string) (types.ContainerJSON, error) {
  13. serverResp, err := cli.get(ctx, "/containers/"+containerID+"/json", nil, nil)
  14. if err != nil {
  15. if serverResp.statusCode == http.StatusNotFound {
  16. return types.ContainerJSON{}, containerNotFoundError{containerID}
  17. }
  18. return types.ContainerJSON{}, err
  19. }
  20. var response types.ContainerJSON
  21. err = json.NewDecoder(serverResp.body).Decode(&response)
  22. ensureReaderClosed(serverResp)
  23. return response, err
  24. }
  25. // ContainerInspectWithRaw returns the container information and it's raw representation.
  26. func (cli *Client) ContainerInspectWithRaw(ctx context.Context, containerID string, getSize bool) (types.ContainerJSON, []byte, error) {
  27. query := url.Values{}
  28. if getSize {
  29. query.Set("size", "1")
  30. }
  31. serverResp, err := cli.get(ctx, "/containers/"+containerID+"/json", query, nil)
  32. if err != nil {
  33. if serverResp.statusCode == http.StatusNotFound {
  34. return types.ContainerJSON{}, nil, containerNotFoundError{containerID}
  35. }
  36. return types.ContainerJSON{}, nil, err
  37. }
  38. defer ensureReaderClosed(serverResp)
  39. body, err := ioutil.ReadAll(serverResp.body)
  40. if err != nil {
  41. return types.ContainerJSON{}, nil, err
  42. }
  43. var response types.ContainerJSON
  44. rdr := bytes.NewReader(body)
  45. err = json.NewDecoder(rdr).Decode(&response)
  46. return response, body, err
  47. }
  48. func (cli *Client) containerInspectWithResponse(ctx context.Context, containerID string, query url.Values) (types.ContainerJSON, *serverResponse, error) {
  49. serverResp, err := cli.get(ctx, "/containers/"+containerID+"/json", nil, nil)
  50. if err != nil {
  51. return types.ContainerJSON{}, serverResp, err
  52. }
  53. var response types.ContainerJSON
  54. err = json.NewDecoder(serverResp.body).Decode(&response)
  55. return response, serverResp, err
  56. }