Browse code

Image commit

Signed-off-by: Daniel Nephin <dnephin@docker.com>

Daniel Nephin authored on 2018/02/22 07:20:58
Showing 2 changed files
... ...
@@ -1,9 +1,7 @@
1 1
 package daemon // import "github.com/docker/docker/daemon"
2 2
 
3 3
 import (
4
-	"encoding/json"
5 4
 	"fmt"
6
-	"io"
7 5
 	"runtime"
8 6
 	"strings"
9 7
 	"time"
... ...
@@ -12,10 +10,6 @@ import (
12 12
 	containertypes "github.com/docker/docker/api/types/container"
13 13
 	"github.com/docker/docker/builder/dockerfile"
14 14
 	"github.com/docker/docker/errdefs"
15
-	"github.com/docker/docker/image"
16
-	"github.com/docker/docker/layer"
17
-	"github.com/docker/docker/pkg/ioutils"
18
-	"github.com/docker/docker/pkg/system"
19 15
 	"github.com/pkg/errors"
20 16
 )
21 17
 
... ...
@@ -190,115 +184,3 @@ func (daemon *Daemon) CreateImageFromContainer(name string, c *backend.CreateIma
190 190
 	containerActions.WithValues("commit").UpdateSince(start)
191 191
 	return id.String(), nil
192 192
 }
193
-
194
-func (daemon *Daemon) commitImage(c backend.CommitConfig) (image.ID, error) {
195
-	layerStore, ok := daemon.layerStores[c.ContainerOS]
196
-	if !ok {
197
-		return "", system.ErrNotSupportedOperatingSystem
198
-	}
199
-	rwTar, err := exportContainerRw(layerStore, c.ContainerID, c.ContainerMountLabel)
200
-	if err != nil {
201
-		return "", err
202
-	}
203
-	defer func() {
204
-		if rwTar != nil {
205
-			rwTar.Close()
206
-		}
207
-	}()
208
-
209
-	var parent *image.Image
210
-	if c.ParentImageID == "" {
211
-		parent = new(image.Image)
212
-		parent.RootFS = image.NewRootFS()
213
-	} else {
214
-		parent, err = daemon.imageStore.Get(image.ID(c.ParentImageID))
215
-		if err != nil {
216
-			return "", err
217
-		}
218
-	}
219
-
220
-	l, err := layerStore.Register(rwTar, parent.RootFS.ChainID())
221
-	if err != nil {
222
-		return "", err
223
-	}
224
-	defer layer.ReleaseAndLog(layerStore, l)
225
-
226
-	cc := image.ChildConfig{
227
-		ContainerID:     c.ContainerID,
228
-		Author:          c.Author,
229
-		Comment:         c.Comment,
230
-		ContainerConfig: c.ContainerConfig,
231
-		Config:          c.Config,
232
-		DiffID:          l.DiffID(),
233
-	}
234
-	config, err := json.Marshal(image.NewChildImage(parent, cc, c.ContainerOS))
235
-	if err != nil {
236
-		return "", err
237
-	}
238
-
239
-	id, err := daemon.imageStore.Create(config)
240
-	if err != nil {
241
-		return "", err
242
-	}
243
-
244
-	if c.ParentImageID != "" {
245
-		if err := daemon.imageStore.SetParent(id, image.ID(c.ParentImageID)); err != nil {
246
-			return "", err
247
-		}
248
-	}
249
-	return id, nil
250
-}
251
-
252
-func exportContainerRw(layerStore layer.Store, id, mountLabel string) (arch io.ReadCloser, err error) {
253
-	rwlayer, err := layerStore.GetRWLayer(id)
254
-	if err != nil {
255
-		return nil, err
256
-	}
257
-	defer func() {
258
-		if err != nil {
259
-			layerStore.ReleaseRWLayer(rwlayer)
260
-		}
261
-	}()
262
-
263
-	// TODO: this mount call is not necessary as we assume that TarStream() should
264
-	// mount the layer if needed. But the Diff() function for windows requests that
265
-	// the layer should be mounted when calling it. So we reserve this mount call
266
-	// until windows driver can implement Diff() interface correctly.
267
-	_, err = rwlayer.Mount(mountLabel)
268
-	if err != nil {
269
-		return nil, err
270
-	}
271
-
272
-	archive, err := rwlayer.TarStream()
273
-	if err != nil {
274
-		rwlayer.Unmount()
275
-		return nil, err
276
-	}
277
-	return ioutils.NewReadCloserWrapper(archive, func() error {
278
-			archive.Close()
279
-			err = rwlayer.Unmount()
280
-			layerStore.ReleaseRWLayer(rwlayer)
281
-			return err
282
-		}),
283
-		nil
284
-}
285
-
286
-// CommitBuildStep is used by the builder to create an image for each step in
287
-// the build.
288
-//
289
-// This method is different from CreateImageFromContainer:
290
-//   * it doesn't attempt to validate container state
291
-//   * it doesn't send a commit action to metrics
292
-//   * it doesn't log a container commit event
293
-//
294
-// This is a temporary shim. Should be removed when builder stops using commit.
295
-func (daemon *Daemon) CommitBuildStep(c backend.CommitConfig) (image.ID, error) {
296
-	container, err := daemon.GetContainer(c.ContainerID)
297
-	if err != nil {
298
-		return "", err
299
-	}
300
-	c.ContainerMountLabel = container.MountLabel
301
-	c.ContainerOS = container.OS
302
-	c.ParentImageID = string(container.ImageID)
303
-	return daemon.commitImage(c)
304
-}
305 193
new file mode 100644
... ...
@@ -0,0 +1,124 @@
0
+package daemon // import "github.com/docker/docker/daemon"
1
+
2
+import (
3
+	"encoding/json"
4
+	"io"
5
+
6
+	"github.com/docker/docker/api/types/backend"
7
+	"github.com/docker/docker/image"
8
+	"github.com/docker/docker/layer"
9
+	"github.com/docker/docker/pkg/ioutils"
10
+	"github.com/docker/docker/pkg/system"
11
+)
12
+
13
+func (daemon *Daemon) commitImage(c backend.CommitConfig) (image.ID, error) {
14
+	layerStore, ok := daemon.layerStores[c.ContainerOS]
15
+	if !ok {
16
+		return "", system.ErrNotSupportedOperatingSystem
17
+	}
18
+	rwTar, err := exportContainerRw(layerStore, c.ContainerID, c.ContainerMountLabel)
19
+	if err != nil {
20
+		return "", err
21
+	}
22
+	defer func() {
23
+		if rwTar != nil {
24
+			rwTar.Close()
25
+		}
26
+	}()
27
+
28
+	var parent *image.Image
29
+	if c.ParentImageID == "" {
30
+		parent = new(image.Image)
31
+		parent.RootFS = image.NewRootFS()
32
+	} else {
33
+		parent, err = daemon.imageStore.Get(image.ID(c.ParentImageID))
34
+		if err != nil {
35
+			return "", err
36
+		}
37
+	}
38
+
39
+	l, err := layerStore.Register(rwTar, parent.RootFS.ChainID())
40
+	if err != nil {
41
+		return "", err
42
+	}
43
+	defer layer.ReleaseAndLog(layerStore, l)
44
+
45
+	cc := image.ChildConfig{
46
+		ContainerID:     c.ContainerID,
47
+		Author:          c.Author,
48
+		Comment:         c.Comment,
49
+		ContainerConfig: c.ContainerConfig,
50
+		Config:          c.Config,
51
+		DiffID:          l.DiffID(),
52
+	}
53
+	config, err := json.Marshal(image.NewChildImage(parent, cc, c.ContainerOS))
54
+	if err != nil {
55
+		return "", err
56
+	}
57
+
58
+	id, err := daemon.imageStore.Create(config)
59
+	if err != nil {
60
+		return "", err
61
+	}
62
+
63
+	if c.ParentImageID != "" {
64
+		if err := daemon.imageStore.SetParent(id, image.ID(c.ParentImageID)); err != nil {
65
+			return "", err
66
+		}
67
+	}
68
+	return id, nil
69
+}
70
+
71
+func exportContainerRw(layerStore layer.Store, id, mountLabel string) (arch io.ReadCloser, err error) {
72
+	rwlayer, err := layerStore.GetRWLayer(id)
73
+	if err != nil {
74
+		return nil, err
75
+	}
76
+	defer func() {
77
+		if err != nil {
78
+			layerStore.ReleaseRWLayer(rwlayer)
79
+		}
80
+	}()
81
+
82
+	// TODO: this mount call is not necessary as we assume that TarStream() should
83
+	// mount the layer if needed. But the Diff() function for windows requests that
84
+	// the layer should be mounted when calling it. So we reserve this mount call
85
+	// until windows driver can implement Diff() interface correctly.
86
+	_, err = rwlayer.Mount(mountLabel)
87
+	if err != nil {
88
+		return nil, err
89
+	}
90
+
91
+	archive, err := rwlayer.TarStream()
92
+	if err != nil {
93
+		rwlayer.Unmount()
94
+		return nil, err
95
+	}
96
+	return ioutils.NewReadCloserWrapper(archive, func() error {
97
+			archive.Close()
98
+			err = rwlayer.Unmount()
99
+			layerStore.ReleaseRWLayer(rwlayer)
100
+			return err
101
+		}),
102
+		nil
103
+}
104
+
105
+// CommitBuildStep is used by the builder to create an image for each step in
106
+// the build.
107
+//
108
+// This method is different from CreateImageFromContainer:
109
+//   * it doesn't attempt to validate container state
110
+//   * it doesn't send a commit action to metrics
111
+//   * it doesn't log a container commit event
112
+//
113
+// This is a temporary shim. Should be removed when builder stops using commit.
114
+func (daemon *Daemon) CommitBuildStep(c backend.CommitConfig) (image.ID, error) {
115
+	container, err := daemon.GetContainer(c.ContainerID)
116
+	if err != nil {
117
+		return "", err
118
+	}
119
+	c.ContainerMountLabel = container.MountLabel
120
+	c.ContainerOS = container.OS
121
+	c.ParentImageID = string(container.ImageID)
122
+	return daemon.commitImage(c)
123
+}