2014-07-31 16:57:21 -04:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/docker/docker/engine"
|
|
|
|
"github.com/docker/docker/image"
|
|
|
|
"github.com/docker/docker/runconfig"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (daemon *Daemon) ContainerCommit(job *engine.Job) engine.Status {
|
|
|
|
if len(job.Args) != 1 {
|
|
|
|
return job.Errorf("Not enough arguments. Usage: %s CONTAINER\n", job.Name)
|
|
|
|
}
|
|
|
|
name := job.Args[0]
|
|
|
|
|
2014-12-16 18:06:35 -05:00
|
|
|
container, err := daemon.Get(name)
|
|
|
|
if err != nil {
|
|
|
|
return job.Error(err)
|
2014-07-31 16:57:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
config = container.Config
|
|
|
|
newConfig runconfig.Config
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := job.GetenvJson("config", &newConfig); err != nil {
|
|
|
|
return job.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := runconfig.Merge(&newConfig, config); err != nil {
|
|
|
|
return job.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
img, err := daemon.Commit(container, job.Getenv("repo"), job.Getenv("tag"), job.Getenv("comment"), job.Getenv("author"), job.GetenvBool("pause"), &newConfig)
|
|
|
|
if err != nil {
|
|
|
|
return job.Error(err)
|
|
|
|
}
|
|
|
|
job.Printf("%s\n", img.ID)
|
|
|
|
return engine.StatusOK
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit creates a new filesystem image from the current state of a container.
|
|
|
|
// The image can optionally be tagged into a repository
|
|
|
|
func (daemon *Daemon) Commit(container *Container, repository, tag, comment, author string, pause bool, config *runconfig.Config) (*image.Image, error) {
|
|
|
|
if pause {
|
|
|
|
container.Pause()
|
|
|
|
defer container.Unpause()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := container.Mount(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer container.Unmount()
|
|
|
|
|
|
|
|
rwTar, err := container.ExportRw()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rwTar.Close()
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2014-10-28 17:06:23 -04:00
|
|
|
img, err := daemon.graph.Create(rwTar, containerID, parentImageID, comment, author, containerConfig, config)
|
2014-07-31 16:57:21 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register the image if needed
|
|
|
|
if repository != "" {
|
|
|
|
if err := daemon.repositories.Set(repository, tag, img.ID, true); err != nil {
|
|
|
|
return img, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return img, nil
|
|
|
|
}
|