container_inspect.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package client // import "github.com/docker/docker/client"
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "io"
  7. "net/url"
  8. "github.com/docker/docker/api/types"
  9. )
  10. // ContainerInspect returns the container information.
  11. func (cli *Client) ContainerInspect(ctx context.Context, containerID string) (types.ContainerJSON, error) {
  12. if containerID == "" {
  13. return types.ContainerJSON{}, objectNotFoundError{object: "container", id: containerID}
  14. }
  15. serverResp, err := cli.get(ctx, "/containers/"+containerID+"/json", nil, nil)
  16. defer ensureReaderClosed(serverResp)
  17. if err != nil {
  18. return types.ContainerJSON{}, err
  19. }
  20. var response types.ContainerJSON
  21. err = json.NewDecoder(serverResp.body).Decode(&response)
  22. return response, err
  23. }
  24. // ContainerInspectWithRaw returns the container information and its raw representation.
  25. func (cli *Client) ContainerInspectWithRaw(ctx context.Context, containerID string, getSize bool) (types.ContainerJSON, []byte, error) {
  26. if containerID == "" {
  27. return types.ContainerJSON{}, nil, objectNotFoundError{object: "container", id: containerID}
  28. }
  29. query := url.Values{}
  30. if getSize {
  31. query.Set("size", "1")
  32. }
  33. serverResp, err := cli.get(ctx, "/containers/"+containerID+"/json", query, nil)
  34. defer ensureReaderClosed(serverResp)
  35. if err != nil {
  36. return types.ContainerJSON{}, nil, err
  37. }
  38. body, err := io.ReadAll(serverResp.body)
  39. if err != nil {
  40. return types.ContainerJSON{}, nil, err
  41. }
  42. var response types.ContainerJSON
  43. rdr := bytes.NewReader(body)
  44. err = json.NewDecoder(rdr).Decode(&response)
  45. return response, body, err
  46. }