2014-07-31 20:57:21 +00:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2015-07-20 17:57:15 +00:00
|
|
|
"github.com/docker/docker/image"
|
2015-11-02 22:37:45 +00:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
2014-07-31 20:57:21 +00:00
|
|
|
"github.com/docker/docker/runconfig"
|
|
|
|
)
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
// ContainerCommitConfig contains build configs for commit operation,
|
|
|
|
// and is used when making a commit with the current state of the container.
|
2015-04-10 20:41:43 +00:00
|
|
|
type ContainerCommitConfig struct {
|
|
|
|
Pause bool
|
|
|
|
Repo string
|
|
|
|
Tag string
|
|
|
|
Author string
|
|
|
|
Comment string
|
2015-04-16 21:26:33 +00:00
|
|
|
Config *runconfig.Config
|
2014-07-31 20:57:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Commit creates a new filesystem image from the current state of a container.
|
2015-07-30 21:01:53 +00:00
|
|
|
// The image can optionally be tagged into a repository.
|
2015-09-29 17:51:40 +00:00
|
|
|
func (daemon *Daemon) Commit(container *Container, c *ContainerCommitConfig) (*image.Image, error) {
|
2015-07-30 21:01:53 +00:00
|
|
|
if c.Pause && !container.isPaused() {
|
2015-11-02 23:39:39 +00:00
|
|
|
daemon.containerPause(container)
|
|
|
|
defer daemon.containerUnpause(container)
|
2014-07-31 20:57:21 +00:00
|
|
|
}
|
|
|
|
|
2015-11-02 22:37:45 +00:00
|
|
|
rwTar, err := daemon.exportContainerRw(container)
|
2014-07-31 20:57:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-24 22:03:53 +00:00
|
|
|
defer func() {
|
|
|
|
if rwTar != nil {
|
|
|
|
rwTar.Close()
|
|
|
|
}
|
|
|
|
}()
|
2014-07-31 20:57:21 +00:00
|
|
|
|
|
|
|
// Create a new image from the container's base layers + a new layer from container changes
|
2015-09-01 08:33:14 +00:00
|
|
|
img, err := daemon.graph.Create(rwTar, container.ID, container.ImageID, c.Comment, c.Author, container.Config, c.Config)
|
2014-07-31 20:57:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register the image if needed
|
2015-06-20 10:40:37 +00:00
|
|
|
if c.Repo != "" {
|
|
|
|
if err := daemon.repositories.Tag(c.Repo, c.Tag, img.ID, true); err != nil {
|
2014-07-31 20:57:21 +00:00
|
|
|
return img, err
|
|
|
|
}
|
|
|
|
}
|
2015-11-03 17:33:13 +00:00
|
|
|
|
|
|
|
daemon.LogContainerEvent(container, "commit")
|
2014-07-31 20:57:21 +00:00
|
|
|
return img, nil
|
|
|
|
}
|
2015-11-02 22:37:45 +00:00
|
|
|
|
|
|
|
func (daemon *Daemon) exportContainerRw(container *Container) (archive.Archive, error) {
|
|
|
|
archive, err := daemon.diff(container)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ioutils.NewReadCloserWrapper(archive, func() error {
|
|
|
|
err := archive.Close()
|
|
|
|
return err
|
|
|
|
}),
|
|
|
|
nil
|
|
|
|
}
|