github.com/uriddle/docker@v0.0.0-20210926094723-4072e6aeb013/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  
    55  	// rawJSON caches the immutable JSON associated with this image.
    56  	rawJSON []byte
    57  
    58  	// computedID is the ID computed from the hash of the image config.
    59  	// Not to be confused with the legacy V1 ID in V1Image.
    60  	computedID ID
    61  }
    62  
    63  // RawJSON returns the immutable JSON associated with the image.
    64  func (img *Image) RawJSON() []byte {
    65  	return img.rawJSON
    66  }
    67  
    68  // ID returns the image's content-addressable ID.
    69  func (img *Image) ID() ID {
    70  	return img.computedID
    71  }
    72  
    73  // MarshalJSON serializes the image to JSON. It sorts the top-level keys so
    74  // that JSON that's been manipulated by a push/pull cycle with a legacy
    75  // registry won't end up with a different key order.
    76  func (img *Image) MarshalJSON() ([]byte, error) {
    77  	type MarshalImage Image
    78  
    79  	pass1, err := json.Marshal(MarshalImage(*img))
    80  	if err != nil {
    81  		return nil, err
    82  	}
    83  
    84  	var c map[string]*json.RawMessage
    85  	if err := json.Unmarshal(pass1, &c); err != nil {
    86  		return nil, err
    87  	}
    88  	return json.Marshal(c)
    89  }
    90  
    91  // History stores build commands that were used to create an image
    92  type History struct {
    93  	// Created timestamp for build point
    94  	Created time.Time `json:"created"`
    95  	// Author of the build point
    96  	Author string `json:"author,omitempty"`
    97  	// CreatedBy keeps the Dockerfile command used while building image.
    98  	CreatedBy string `json:"created_by,omitempty"`
    99  	// Comment is custom message set by the user when creating the image.
   100  	Comment string `json:"comment,omitempty"`
   101  	// EmptyLayer is set to true if this history item did not generate a
   102  	// layer. Otherwise, the history item is associated with the next
   103  	// layer in the RootFS section.
   104  	EmptyLayer bool `json:"empty_layer,omitempty"`
   105  }
   106  
   107  // Exporter provides interface for exporting and importing images
   108  type Exporter interface {
   109  	Load(io.ReadCloser, io.Writer) error
   110  	// TODO: Load(net.Context, io.ReadCloser, <- chan StatusMessage) error
   111  	Save([]string, io.Writer) error
   112  }
   113  
   114  // NewFromJSON creates an Image configuration from json.
   115  func NewFromJSON(src []byte) (*Image, error) {
   116  	img := &Image{}
   117  
   118  	if err := json.Unmarshal(src, img); err != nil {
   119  		return nil, err
   120  	}
   121  	if img.RootFS == nil {
   122  		return nil, errors.New("Invalid image JSON, no RootFS key.")
   123  	}
   124  
   125  	img.rawJSON = src
   126  
   127  	return img, nil
   128  }