2014-07-31 16:57:21 -04:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2015-11-06 13:05:00 -05:00
|
|
|
"fmt"
|
|
|
|
"runtime"
|
|
|
|
|
2015-07-20 13:57:15 -04:00
|
|
|
"github.com/docker/docker/image"
|
2015-11-02 17:37:45 -05:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
2014-07-31 16:57:21 -04:00
|
|
|
"github.com/docker/docker/runconfig"
|
|
|
|
)
|
|
|
|
|
2015-07-30 17:01:53 -04: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 16:41:43 -04:00
|
|
|
type ContainerCommitConfig struct {
|
|
|
|
Pause bool
|
|
|
|
Repo string
|
|
|
|
Tag string
|
|
|
|
Author string
|
|
|
|
Comment string
|
2015-11-06 13:05:00 -05:00
|
|
|
// merge container config into commit config before commit
|
|
|
|
MergeConfigs bool
|
|
|
|
Config *runconfig.Config
|
2014-07-31 16:57:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Commit creates a new filesystem image from the current state of a container.
|
2015-07-30 17:01:53 -04:00
|
|
|
// The image can optionally be tagged into a repository.
|
2015-11-06 13:05:00 -05:00
|
|
|
func (daemon *Daemon) Commit(name string, c *ContainerCommitConfig) (*image.Image, error) {
|
|
|
|
container, err := daemon.Get(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// It is not possible to commit a running container on Windows
|
|
|
|
if runtime.GOOS == "windows" && container.IsRunning() {
|
|
|
|
return nil, fmt.Errorf("Windows does not support commit of a running container")
|
|
|
|
}
|
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
if c.Pause && !container.isPaused() {
|
2015-11-02 18:39:39 -05:00
|
|
|
daemon.containerPause(container)
|
|
|
|
defer daemon.containerUnpause(container)
|
2014-07-31 16:57:21 -04:00
|
|
|
}
|
|
|
|
|
2015-11-06 13:05:00 -05:00
|
|
|
if c.MergeConfigs {
|
|
|
|
if err := runconfig.Merge(c.Config, container.Config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-02 17:37:45 -05:00
|
|
|
rwTar, err := daemon.exportContainerRw(container)
|
2014-07-31 16:57:21 -04:00
|
|
|
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
|
2015-09-01 04:33:14 -04:00
|
|
|
img, err := daemon.graph.Create(rwTar, container.ID, container.ImageID, c.Comment, c.Author, container.Config, 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-11-03 12:33:13 -05:00
|
|
|
|
|
|
|
daemon.LogContainerEvent(container, "commit")
|
2014-07-31 16:57:21 -04:00
|
|
|
return img, nil
|
|
|
|
}
|
2015-11-02 17:37:45 -05: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
|
|
|
|
}
|