Tizen_4.0 base
[platform/upstream/docker-engine.git] / daemon / commit.go
1 package daemon
2
3 import (
4         "encoding/json"
5         "io"
6         "runtime"
7         "strings"
8         "time"
9
10         "github.com/docker/distribution/reference"
11         "github.com/docker/docker/api/types/backend"
12         containertypes "github.com/docker/docker/api/types/container"
13         "github.com/docker/docker/builder/dockerfile"
14         "github.com/docker/docker/container"
15         "github.com/docker/docker/image"
16         "github.com/docker/docker/layer"
17         "github.com/docker/docker/pkg/ioutils"
18         "github.com/pkg/errors"
19 )
20
21 // merge merges two Config, the image container configuration (defaults values),
22 // and the user container configuration, either passed by the API or generated
23 // by the cli.
24 // It will mutate the specified user configuration (userConf) with the image
25 // configuration where the user configuration is incomplete.
26 func merge(userConf, imageConf *containertypes.Config) error {
27         if userConf.User == "" {
28                 userConf.User = imageConf.User
29         }
30         if len(userConf.ExposedPorts) == 0 {
31                 userConf.ExposedPorts = imageConf.ExposedPorts
32         } else if imageConf.ExposedPorts != nil {
33                 for port := range imageConf.ExposedPorts {
34                         if _, exists := userConf.ExposedPorts[port]; !exists {
35                                 userConf.ExposedPorts[port] = struct{}{}
36                         }
37                 }
38         }
39
40         if len(userConf.Env) == 0 {
41                 userConf.Env = imageConf.Env
42         } else {
43                 for _, imageEnv := range imageConf.Env {
44                         found := false
45                         imageEnvKey := strings.Split(imageEnv, "=")[0]
46                         for _, userEnv := range userConf.Env {
47                                 userEnvKey := strings.Split(userEnv, "=")[0]
48                                 if runtime.GOOS == "windows" {
49                                         // Case insensitive environment variables on Windows
50                                         imageEnvKey = strings.ToUpper(imageEnvKey)
51                                         userEnvKey = strings.ToUpper(userEnvKey)
52                                 }
53                                 if imageEnvKey == userEnvKey {
54                                         found = true
55                                         break
56                                 }
57                         }
58                         if !found {
59                                 userConf.Env = append(userConf.Env, imageEnv)
60                         }
61                 }
62         }
63
64         if userConf.Labels == nil {
65                 userConf.Labels = map[string]string{}
66         }
67         for l, v := range imageConf.Labels {
68                 if _, ok := userConf.Labels[l]; !ok {
69                         userConf.Labels[l] = v
70                 }
71         }
72
73         if len(userConf.Entrypoint) == 0 {
74                 if len(userConf.Cmd) == 0 {
75                         userConf.Cmd = imageConf.Cmd
76                         userConf.ArgsEscaped = imageConf.ArgsEscaped
77                 }
78
79                 if userConf.Entrypoint == nil {
80                         userConf.Entrypoint = imageConf.Entrypoint
81                 }
82         }
83         if imageConf.Healthcheck != nil {
84                 if userConf.Healthcheck == nil {
85                         userConf.Healthcheck = imageConf.Healthcheck
86                 } else {
87                         if len(userConf.Healthcheck.Test) == 0 {
88                                 userConf.Healthcheck.Test = imageConf.Healthcheck.Test
89                         }
90                         if userConf.Healthcheck.Interval == 0 {
91                                 userConf.Healthcheck.Interval = imageConf.Healthcheck.Interval
92                         }
93                         if userConf.Healthcheck.Timeout == 0 {
94                                 userConf.Healthcheck.Timeout = imageConf.Healthcheck.Timeout
95                         }
96                         if userConf.Healthcheck.StartPeriod == 0 {
97                                 userConf.Healthcheck.StartPeriod = imageConf.Healthcheck.StartPeriod
98                         }
99                         if userConf.Healthcheck.Retries == 0 {
100                                 userConf.Healthcheck.Retries = imageConf.Healthcheck.Retries
101                         }
102                 }
103         }
104
105         if userConf.WorkingDir == "" {
106                 userConf.WorkingDir = imageConf.WorkingDir
107         }
108         if len(userConf.Volumes) == 0 {
109                 userConf.Volumes = imageConf.Volumes
110         } else {
111                 for k, v := range imageConf.Volumes {
112                         userConf.Volumes[k] = v
113                 }
114         }
115
116         if userConf.StopSignal == "" {
117                 userConf.StopSignal = imageConf.StopSignal
118         }
119         return nil
120 }
121
122 // Commit creates a new filesystem image from the current state of a container.
123 // The image can optionally be tagged into a repository.
124 func (daemon *Daemon) Commit(name string, c *backend.ContainerCommitConfig) (string, error) {
125         start := time.Now()
126         container, err := daemon.GetContainer(name)
127         if err != nil {
128                 return "", err
129         }
130
131         // It is not possible to commit a running container on Windows and on Solaris.
132         if (runtime.GOOS == "windows" || runtime.GOOS == "solaris") && container.IsRunning() {
133                 return "", errors.Errorf("%+v does not support commit of a running container", runtime.GOOS)
134         }
135
136         if c.Pause && !container.IsPaused() {
137                 daemon.containerPause(container)
138                 defer daemon.containerUnpause(container)
139         }
140
141         newConfig, err := dockerfile.BuildFromConfig(c.Config, c.Changes)
142         if err != nil {
143                 return "", err
144         }
145
146         if c.MergeConfigs {
147                 if err := merge(newConfig, container.Config); err != nil {
148                         return "", err
149                 }
150         }
151
152         rwTar, err := daemon.exportContainerRw(container)
153         if err != nil {
154                 return "", err
155         }
156         defer func() {
157                 if rwTar != nil {
158                         rwTar.Close()
159                 }
160         }()
161
162         var parent *image.Image
163         if container.ImageID == "" {
164                 parent = new(image.Image)
165                 parent.RootFS = image.NewRootFS()
166         } else {
167                 parent, err = daemon.stores[container.Platform].imageStore.Get(container.ImageID)
168                 if err != nil {
169                         return "", err
170                 }
171         }
172
173         l, err := daemon.stores[container.Platform].layerStore.Register(rwTar, parent.RootFS.ChainID(), layer.Platform(container.Platform))
174         if err != nil {
175                 return "", err
176         }
177         defer layer.ReleaseAndLog(daemon.stores[container.Platform].layerStore, l)
178
179         containerConfig := c.ContainerConfig
180         if containerConfig == nil {
181                 containerConfig = container.Config
182         }
183         cc := image.ChildConfig{
184                 ContainerID:     container.ID,
185                 Author:          c.Author,
186                 Comment:         c.Comment,
187                 ContainerConfig: containerConfig,
188                 Config:          newConfig,
189                 DiffID:          l.DiffID(),
190         }
191         config, err := json.Marshal(image.NewChildImage(parent, cc, container.Platform))
192         if err != nil {
193                 return "", err
194         }
195
196         id, err := daemon.stores[container.Platform].imageStore.Create(config)
197         if err != nil {
198                 return "", err
199         }
200
201         if container.ImageID != "" {
202                 if err := daemon.stores[container.Platform].imageStore.SetParent(id, container.ImageID); err != nil {
203                         return "", err
204                 }
205         }
206
207         imageRef := ""
208         if c.Repo != "" {
209                 newTag, err := reference.ParseNormalizedNamed(c.Repo) // todo: should move this to API layer
210                 if err != nil {
211                         return "", err
212                 }
213                 if !reference.IsNameOnly(newTag) {
214                         return "", errors.Errorf("unexpected repository name: %s", c.Repo)
215                 }
216                 if c.Tag != "" {
217                         if newTag, err = reference.WithTag(newTag, c.Tag); err != nil {
218                                 return "", err
219                         }
220                 }
221                 if err := daemon.TagImageWithReference(id, container.Platform, newTag); err != nil {
222                         return "", err
223                 }
224                 imageRef = reference.FamiliarString(newTag)
225         }
226
227         attributes := map[string]string{
228                 "comment":  c.Comment,
229                 "imageID":  id.String(),
230                 "imageRef": imageRef,
231         }
232         daemon.LogContainerEventWithAttributes(container, "commit", attributes)
233         containerActions.WithValues("commit").UpdateSince(start)
234         return id.String(), nil
235 }
236
237 func (daemon *Daemon) exportContainerRw(container *container.Container) (io.ReadCloser, error) {
238         if err := daemon.Mount(container); err != nil {
239                 return nil, err
240         }
241
242         archive, err := container.RWLayer.TarStream()
243         if err != nil {
244                 daemon.Unmount(container) // logging is already handled in the `Unmount` function
245                 return nil, err
246         }
247         return ioutils.NewReadCloserWrapper(archive, func() error {
248                         archive.Close()
249                         return container.RWLayer.Unmount()
250                 }),
251                 nil
252 }