github.com/slene/docker@v1.8.0-rc1/daemon/commit.go (about)

     1  package daemon
     2  
     3  import (
     4  	"github.com/docker/docker/image"
     5  	"github.com/docker/docker/runconfig"
     6  )
     7  
     8  type ContainerCommitConfig struct {
     9  	Pause   bool
    10  	Repo    string
    11  	Tag     string
    12  	Author  string
    13  	Comment string
    14  	Config  *runconfig.Config
    15  }
    16  
    17  // Commit creates a new filesystem image from the current state of a container.
    18  // The image can optionally be tagged into a repository
    19  func (daemon *Daemon) Commit(container *Container, c *ContainerCommitConfig) (*image.Image, error) {
    20  	if c.Pause && !container.IsPaused() {
    21  		container.Pause()
    22  		defer container.Unpause()
    23  	}
    24  
    25  	rwTar, err := container.ExportRw()
    26  	if err != nil {
    27  		return nil, err
    28  	}
    29  	defer func() {
    30  		if rwTar != nil {
    31  			rwTar.Close()
    32  		}
    33  	}()
    34  
    35  	// Create a new image from the container's base layers + a new layer from container changes
    36  	var (
    37  		containerID, parentImageID string
    38  		containerConfig            *runconfig.Config
    39  	)
    40  
    41  	if container != nil {
    42  		containerID = container.ID
    43  		parentImageID = container.ImageID
    44  		containerConfig = container.Config
    45  	}
    46  
    47  	img, err := daemon.graph.Create(rwTar, containerID, parentImageID, c.Comment, c.Author, containerConfig, c.Config)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  
    52  	// Register the image if needed
    53  	if c.Repo != "" {
    54  		if err := daemon.repositories.Tag(c.Repo, c.Tag, img.ID, true); err != nil {
    55  			return img, err
    56  		}
    57  	}
    58  	container.LogEvent("commit")
    59  	return img, nil
    60  }