docker镜像格式

本文探讨了Docker镜像的导出过程,包括使用`docker save`命令生成`.tar`档案,解压后包含manifest、config文件及Layers目录。config文件存储镜像配置,Layers目录下有json和VERSION文件,json文件格式遵循V1Image结构。docker save流程中,会逐一保存image的各层,并创建manifest文件来组织这些信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

导出镜像

docker save -o xxx.tar image1 image2 ….imagen
解压tar包之后,有下面一些文件

manifest

type manifestItem struct {
    Config       string
    RepoTags     []string
    Layers       []string
    Parent       image.ID                                 `json:",omitempty"`
    LayerSources map[layer.DiffID]distribution.Descriptor `json:",omitempty"`
}

config是镜像的config文件
RepoTags一般都是null
Layres是各个层的目录

config文件的格式

// V1Image stores the V1 image configuration.
type V1Image struct {
    // ID is a unique 64 character identifier of the image
    ID string `json:"id,omitempty"`
    // Parent is the ID of the parent image
    Parent string `json:"parent,omitempty"`
    // Comment is the commit message that was set when committing the image
    Comment string `json:"comment,omitempty"`
    // Created is the timestamp at which the image was created
    Created time.Time `json:"created"`
    // Container is the id of the container used to commit
    Container string `json:"container,omitempty"`
    // ContainerConfig is the configuration of the container that is committed into the image
    ContainerConfig container.Config `json:"container_config,omitempty"`
    // DockerVersion specifies the version of Docker that was used to build the image
    DockerVersion string `json:"docker_version,omitempty"`
    // Author is the name of the author that was specified when committing the image
    Author string `json:"author,omitempty"`
    // Config is the configuration of the container received from the client
    Config *container.Config `json:"config,omitempty"`
    // Architecture is the hardware that the image is built and runs on
    Architecture string `json:"architecture,omitempty"`
    // OS is the operating system used to build and run the image
    OS string `json:"os,omitempty"`
    // Size is the total size of the image including all layers it is composed of
    Size int64 `json:",omitempty"`
}

// Image stores the image configuration
type Image struct {
    V1Image
    Parent     ID        `json:"parent,omitempty"`
    RootFS     *RootFS   `json:"rootfs,omitempty"`
    History    []History `json:"history,omitempty"`
    OSVersion  string    `json:"os.version,omitempty"`
    OSFeatures []string  `json:"os.features,omitempty"`

    // rawJSON caches the immutable JSON associated with this image.
    rawJSON []byte

    // computedID is the ID computed from the hash of the image config.
    // Not to be confused with the legacy V1 ID in V1Image.
    computedID ID
}

Layer层json文件格式

layer层中有json和VERSION两个文件
json文件的格式就是V1Image

docker save流程

Save(names []string, outStream io.Writer)
func (s *saveSession) save(outStream io.Writer) error   {
   for id, imageDescr := range s.images {
        foreignSrcs, err := s.saveImage(id)
    }
    ...生成manifest文件
    fs, err := archive.Tar(tempDir, archive.Uncompressed)
}
func (s *saveSession) saveImage(id image.ID) (map[layer.DiffID]distribution.Descriptor, error) {
     for i := range img.RootFS.DiffIDs {        
        v1ID, err := v1.CreateID(v1Img, rootFS.ChainID(), parent)
        v1Img.ID = v1ID.Hex()       
        src, err := s.saveLayer(rootFS.ChainID(), v1Img, img.Created)
        }
    configFile := filepath.Join(s.outDir, id.Digest().Hex()+".json")
    if err := ioutil.WriteFile(configFile, img.RawJSON(), 0644); err != nil {
        return nil, err
    }
}
func (s *saveSession) saveLayer(id layer.ChainID, legacyImg image.V1Image, createdTime time.Time) (distribution.Descriptor, error) {

首先save列表参数中所有的image,然后生成manifest文件
每个image保存的过程是:
保存每一个层,每个层中有一个v1Img结构
生成config json文件

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值