github.com/squaremo/docker@v1.3.2-0.20150516120342-42cfc9554972/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  	Changes []string
    15  	Config  *runconfig.Config
    16  }
    17  
    18  // Commit creates a new filesystem image from the current state of a container.
    19  // The image can optionally be tagged into a repository
    20  func (daemon *Daemon) Commit(container *Container, repository, tag, comment, author string, pause bool, config *runconfig.Config) (*image.Image, error) {
    21  	if pause && !container.IsPaused() {
    22  		container.Pause()
    23  		defer container.Unpause()
    24  	}
    25  
    26  	if err := container.Mount(); err != nil {
    27  		return nil, err
    28  	}
    29  	defer container.Unmount()
    30  
    31  	rwTar, err := container.ExportRw()
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  	defer func() {
    36  		if rwTar != nil {
    37  			rwTar.Close()
    38  		}
    39  	}()
    40  
    41  	// Create a new image from the container's base layers + a new layer from container changes
    42  	var (
    43  		containerID, parentImageID string
    44  		containerConfig            *runconfig.Config
    45  	)
    46  
    47  	if container != nil {
    48  		containerID = container.ID
    49  		parentImageID = container.ImageID
    50  		containerConfig = container.Config
    51  	}
    52  
    53  	img, err := daemon.graph.Create(rwTar, containerID, parentImageID, comment, author, containerConfig, config)
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  
    58  	// Register the image if needed
    59  	if repository != "" {
    60  		if err := daemon.repositories.Tag(repository, tag, img.ID, true); err != nil {
    61  			return img, err
    62  		}
    63  	}
    64  	return img, nil
    65  }