github.com/sijibomii/docker@v0.0.0-20231230191044-5cf6ca554647/image/image.go (about)

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