github.com/feiyang21687/docker@v1.5.0/daemon/commit.go (about)

     1  package daemon
     2  
     3  import (
     4  	"github.com/docker/docker/engine"
     5  	"github.com/docker/docker/image"
     6  	"github.com/docker/docker/runconfig"
     7  )
     8  
     9  func (daemon *Daemon) ContainerCommit(job *engine.Job) engine.Status {
    10  	if len(job.Args) != 1 {
    11  		return job.Errorf("Not enough arguments. Usage: %s CONTAINER\n", job.Name)
    12  	}
    13  	name := job.Args[0]
    14  
    15  	container := daemon.Get(name)
    16  	if container == nil {
    17  		return job.Errorf("No such container: %s", name)
    18  	}
    19  
    20  	var (
    21  		config    = container.Config
    22  		newConfig runconfig.Config
    23  	)
    24  
    25  	if err := job.GetenvJson("config", &newConfig); err != nil {
    26  		return job.Error(err)
    27  	}
    28  
    29  	if err := runconfig.Merge(&newConfig, config); err != nil {
    30  		return job.Error(err)
    31  	}
    32  
    33  	img, err := daemon.Commit(container, job.Getenv("repo"), job.Getenv("tag"), job.Getenv("comment"), job.Getenv("author"), job.GetenvBool("pause"), &newConfig)
    34  	if err != nil {
    35  		return job.Error(err)
    36  	}
    37  	job.Printf("%s\n", img.ID)
    38  	return engine.StatusOK
    39  }
    40  
    41  // Commit creates a new filesystem image from the current state of a container.
    42  // The image can optionally be tagged into a repository
    43  func (daemon *Daemon) Commit(container *Container, repository, tag, comment, author string, pause bool, config *runconfig.Config) (*image.Image, error) {
    44  	if pause {
    45  		container.Pause()
    46  		defer container.Unpause()
    47  	}
    48  
    49  	if err := container.Mount(); err != nil {
    50  		return nil, err
    51  	}
    52  	defer container.Unmount()
    53  
    54  	rwTar, err := container.ExportRw()
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  	defer rwTar.Close()
    59  
    60  	// Create a new image from the container's base layers + a new layer from container changes
    61  	var (
    62  		containerID, parentImageID string
    63  		containerConfig            *runconfig.Config
    64  	)
    65  
    66  	if container != nil {
    67  		containerID = container.ID
    68  		parentImageID = container.ImageID
    69  		containerConfig = container.Config
    70  	}
    71  
    72  	img, err := daemon.graph.Create(rwTar, containerID, parentImageID, comment, author, containerConfig, config)
    73  	if err != nil {
    74  		return nil, err
    75  	}
    76  
    77  	// Register the image if needed
    78  	if repository != "" {
    79  		if err := daemon.repositories.Set(repository, tag, img.ID, true); err != nil {
    80  			return img, err
    81  		}
    82  	}
    83  	return img, nil
    84  }