forked from moby/moby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commit.go
60 lines (52 loc) · 1.35 KB
/
commit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package daemon
import (
"github.com/docker/docker/image"
"github.com/docker/docker/runconfig"
)
type ContainerCommitConfig struct {
Pause bool
Repo string
Tag string
Author string
Comment string
Config *runconfig.Config
}
// 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, c *ContainerCommitConfig) (*image.Image, error) {
if c.Pause && !container.IsPaused() {
container.Pause()
defer container.Unpause()
}
rwTar, err := container.ExportRw()
if err != nil {
return nil, err
}
defer func() {
if rwTar != nil {
rwTar.Close()
}
}()
// Create a new image from the container's base layers + a new layer from container changes
var (
containerID, parentImageID string
containerConfig *runconfig.Config
)
if container != nil {
containerID = container.ID
parentImageID = container.ImageID
containerConfig = container.Config
}
img, err := daemon.graph.Create(rwTar, containerID, parentImageID, c.Comment, c.Author, containerConfig, c.Config)
if err != nil {
return nil, err
}
// Register the image if needed
if c.Repo != "" {
if err := daemon.repositories.Tag(c.Repo, c.Tag, img.ID, true); err != nil {
return img, err
}
}
container.LogEvent("commit")
return img, nil
}