image.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package image
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "io"
  6. "time"
  7. "github.com/docker/distribution/digest"
  8. "github.com/docker/engine-api/types/container"
  9. )
  10. // ID is the content-addressable ID of an image.
  11. type ID digest.Digest
  12. func (id ID) String() string {
  13. return digest.Digest(id).String()
  14. }
  15. // V1Image stores the V1 image configuration.
  16. type V1Image struct {
  17. // ID a unique 64 character identifier of the image
  18. ID string `json:"id,omitempty"`
  19. // Parent id of the image
  20. Parent string `json:"parent,omitempty"`
  21. // Comment user added comment
  22. Comment string `json:"comment,omitempty"`
  23. // Created timestamp when image was created
  24. Created time.Time `json:"created"`
  25. // Container is the id of the container used to commit
  26. Container string `json:"container,omitempty"`
  27. // ContainerConfig is the configuration of the container that is committed into the image
  28. ContainerConfig container.Config `json:"container_config,omitempty"`
  29. // DockerVersion specifies version on which image is built
  30. DockerVersion string `json:"docker_version,omitempty"`
  31. // Author of the image
  32. Author string `json:"author,omitempty"`
  33. // Config is the configuration of the container received from the client
  34. Config *container.Config `json:"config,omitempty"`
  35. // Architecture is the hardware that the image is build and runs on
  36. Architecture string `json:"architecture,omitempty"`
  37. // OS is the operating system used to build and run the image
  38. OS string `json:"os,omitempty"`
  39. // Size is the total size of the image including all layers it is composed of
  40. Size int64 `json:",omitempty"`
  41. }
  42. // Image stores the image configuration
  43. type Image struct {
  44. V1Image
  45. Parent ID `json:"parent,omitempty"`
  46. RootFS *RootFS `json:"rootfs,omitempty"`
  47. History []History `json:"history,omitempty"`
  48. OSVersion string `json:"os.version,omitempty"`
  49. OSFeatures []string `json:"os.features,omitempty"`
  50. // rawJSON caches the immutable JSON associated with this image.
  51. rawJSON []byte
  52. // computedID is the ID computed from the hash of the image config.
  53. // Not to be confused with the legacy V1 ID in V1Image.
  54. computedID ID
  55. }
  56. // RawJSON returns the immutable JSON associated with the image.
  57. func (img *Image) RawJSON() []byte {
  58. return img.rawJSON
  59. }
  60. // ID returns the image's content-addressable ID.
  61. func (img *Image) ID() ID {
  62. return img.computedID
  63. }
  64. // ImageID stringizes ID.
  65. func (img *Image) ImageID() string {
  66. return string(img.ID())
  67. }
  68. // RunConfig returns the image's container config.
  69. func (img *Image) RunConfig() *container.Config {
  70. return img.Config
  71. }
  72. // MarshalJSON serializes the image to JSON. It sorts the top-level keys so
  73. // that JSON that's been manipulated by a push/pull cycle with a legacy
  74. // registry won't end up with a different key order.
  75. func (img *Image) MarshalJSON() ([]byte, error) {
  76. type MarshalImage Image
  77. pass1, err := json.Marshal(MarshalImage(*img))
  78. if err != nil {
  79. return nil, err
  80. }
  81. var c map[string]*json.RawMessage
  82. if err := json.Unmarshal(pass1, &c); err != nil {
  83. return nil, err
  84. }
  85. return json.Marshal(c)
  86. }
  87. // History stores build commands that were used to create an image
  88. type History struct {
  89. // Created timestamp for build point
  90. Created time.Time `json:"created"`
  91. // Author of the build point
  92. Author string `json:"author,omitempty"`
  93. // CreatedBy keeps the Dockerfile command used while building image.
  94. CreatedBy string `json:"created_by,omitempty"`
  95. // Comment is custom message set by the user when creating the image.
  96. Comment string `json:"comment,omitempty"`
  97. // EmptyLayer is set to true if this history item did not generate a
  98. // layer. Otherwise, the history item is associated with the next
  99. // layer in the RootFS section.
  100. EmptyLayer bool `json:"empty_layer,omitempty"`
  101. }
  102. // Exporter provides interface for exporting and importing images
  103. type Exporter interface {
  104. Load(io.ReadCloser, io.Writer, bool) error
  105. // TODO: Load(net.Context, io.ReadCloser, <- chan StatusMessage) error
  106. Save([]string, io.Writer) error
  107. }
  108. // NewFromJSON creates an Image configuration from json.
  109. func NewFromJSON(src []byte) (*Image, error) {
  110. img := &Image{}
  111. if err := json.Unmarshal(src, img); err != nil {
  112. return nil, err
  113. }
  114. if img.RootFS == nil {
  115. return nil, errors.New("Invalid image JSON, no RootFS key.")
  116. }
  117. img.rawJSON = src
  118. return img, nil
  119. }