2014-07-31 16:57:21 -04:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/docker/docker/image"
|
|
|
|
"github.com/docker/docker/runconfig"
|
|
|
|
)
|
|
|
|
|
2015-04-10 16:41:43 -04:00
|
|
|
type ContainerCommitConfig struct {
|
|
|
|
Pause bool
|
|
|
|
Repo string
|
|
|
|
Tag string
|
|
|
|
Author string
|
|
|
|
Comment string
|
2015-04-16 17:26:33 -04:00
|
|
|
Config *runconfig.Config
|
2014-07-31 16:57:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Commit creates a new filesystem image from the current state of a container.
|
|
|
|
// The image can optionally be tagged into a repository
|
2015-06-20 06:40:37 -04:00
|
|
|
func (daemon *Daemon) Commit(container *Container, c *ContainerCommitConfig) (*image.Image, error) {
|
|
|
|
if c.Pause && !container.IsPaused() {
|
2014-07-31 16:57:21 -04:00
|
|
|
container.Pause()
|
|
|
|
defer container.Unpause()
|
|
|
|
}
|
|
|
|
|
|
|
|
rwTar, err := container.ExportRw()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-24 18:03:53 -04:00
|
|
|
defer func() {
|
|
|
|
if rwTar != nil {
|
|
|
|
rwTar.Close()
|
|
|
|
}
|
|
|
|
}()
|
2014-07-31 16:57:21 -04:00
|
|
|
|
|
|
|
// Create a new image from the container's base layers + a new layer from container changes
|
|
|
|
var (
|
2014-10-28 17:06:23 -04:00
|
|
|
containerID, parentImageID string
|
|
|
|
containerConfig *runconfig.Config
|
2014-07-31 16:57:21 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
if container != nil {
|
|
|
|
containerID = container.ID
|
2014-10-28 17:06:23 -04:00
|
|
|
parentImageID = container.ImageID
|
2014-07-31 16:57:21 -04:00
|
|
|
containerConfig = container.Config
|
|
|
|
}
|
|
|
|
|
2015-06-20 06:40:37 -04:00
|
|
|
img, err := daemon.graph.Create(rwTar, containerID, parentImageID, c.Comment, c.Author, containerConfig, c.Config)
|
2014-07-31 16:57:21 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register the image if needed
|
2015-06-20 06:40:37 -04:00
|
|
|
if c.Repo != "" {
|
|
|
|
if err := daemon.repositories.Tag(c.Repo, c.Tag, img.ID, true); err != nil {
|
2014-07-31 16:57:21 -04:00
|
|
|
return img, err
|
|
|
|
}
|
|
|
|
}
|
2015-05-24 14:19:39 -04:00
|
|
|
container.LogEvent("commit")
|
2014-07-31 16:57:21 -04:00
|
|
|
return img, nil
|
|
|
|
}
|