Browse code

Use name instead of container in Commit

It will make daemon interface separation easier later.

Signed-off-by: Alexander Morozov <lk4d4@docker.com>

Alexander Morozov authored on 2015/11/07 03:05:00
Showing 5 changed files
... ...
@@ -114,7 +114,7 @@ type Docker interface {
114 114
 	// Remove removes a container specified by `id`.
115 115
 	Remove(id string, cfg *daemon.ContainerRmConfig) error
116 116
 	// Commit creates a new Docker image from an existing Docker container.
117
-	Commit(*daemon.Container, *daemon.ContainerCommitConfig) (*image.Image, error)
117
+	Commit(string, *daemon.ContainerCommitConfig) (*image.Image, error)
118 118
 	// Copy copies/extracts a source FileInfo to a destination path inside a container
119 119
 	// specified by a container object.
120 120
 	// TODO: make an Extract method instead of passing `decompress`
... ...
@@ -6,7 +6,6 @@ import (
6 6
 	"io"
7 7
 	"io/ioutil"
8 8
 	"os"
9
-	"runtime"
10 9
 	"strings"
11 10
 	"sync"
12 11
 
... ...
@@ -258,16 +257,6 @@ func BuildFromConfig(config *runconfig.Config, changes []string) (*runconfig.Con
258 258
 // Commit will create a new image from a container's changes
259 259
 // TODO: remove daemon, make Commit a method on *Builder ?
260 260
 func Commit(containerName string, d *daemon.Daemon, c *CommitConfig) (string, error) {
261
-	container, err := d.Get(containerName)
262
-	if err != nil {
263
-		return "", err
264
-	}
265
-
266
-	// It is not possible to commit a running container on Windows
267
-	if runtime.GOOS == "windows" && container.IsRunning() {
268
-		return "", fmt.Errorf("Windows does not support commit of a running container")
269
-	}
270
-
271 261
 	if c.Config == nil {
272 262
 		c.Config = &runconfig.Config{}
273 263
 	}
... ...
@@ -277,20 +266,17 @@ func Commit(containerName string, d *daemon.Daemon, c *CommitConfig) (string, er
277 277
 		return "", err
278 278
 	}
279 279
 
280
-	if err := runconfig.Merge(newConfig, container.Config); err != nil {
281
-		return "", err
282
-	}
283
-
284 280
 	commitCfg := &daemon.ContainerCommitConfig{
285
-		Pause:   c.Pause,
286
-		Repo:    c.Repo,
287
-		Tag:     c.Tag,
288
-		Author:  c.Author,
289
-		Comment: c.Comment,
290
-		Config:  newConfig,
281
+		Pause:        c.Pause,
282
+		Repo:         c.Repo,
283
+		Tag:          c.Tag,
284
+		Author:       c.Author,
285
+		Comment:      c.Comment,
286
+		Config:       newConfig,
287
+		MergeConfigs: true,
291 288
 	}
292 289
 
293
-	img, err := d.Commit(container, commitCfg)
290
+	img, err := d.Commit(containerName, commitCfg)
294 291
 	if err != nil {
295 292
 		return "", err
296 293
 	}
... ...
@@ -60,7 +60,6 @@ func (b *Builder) commit(id string, autoCmd *stringutils.StrSlice, comment strin
60 60
 		} else if hit {
61 61
 			return nil
62 62
 		}
63
-
64 63
 		container, err := b.create()
65 64
 		if err != nil {
66 65
 			return err
... ...
@@ -73,11 +72,6 @@ func (b *Builder) commit(id string, autoCmd *stringutils.StrSlice, comment strin
73 73
 		defer b.docker.Unmount(container)
74 74
 	}
75 75
 
76
-	container, err := b.docker.Container(id)
77
-	if err != nil {
78
-		return err
79
-	}
80
-
81 76
 	// Note: Actually copy the struct
82 77
 	autoConfig := *b.runConfig
83 78
 	autoConfig.Cmd = autoCmd
... ...
@@ -89,7 +83,7 @@ func (b *Builder) commit(id string, autoCmd *stringutils.StrSlice, comment strin
89 89
 	}
90 90
 
91 91
 	// Commit the container
92
-	image, err := b.docker.Commit(container, commitCfg)
92
+	image, err := b.docker.Commit(id, commitCfg)
93 93
 	if err != nil {
94 94
 		return err
95 95
 	}
... ...
@@ -1,6 +1,9 @@
1 1
 package daemon
2 2
 
3 3
 import (
4
+	"fmt"
5
+	"runtime"
6
+
4 7
 	"github.com/docker/docker/image"
5 8
 	"github.com/docker/docker/pkg/archive"
6 9
 	"github.com/docker/docker/pkg/ioutils"
... ...
@@ -15,17 +18,35 @@ type ContainerCommitConfig struct {
15 15
 	Tag     string
16 16
 	Author  string
17 17
 	Comment string
18
-	Config  *runconfig.Config
18
+	// merge container config into commit config before commit
19
+	MergeConfigs bool
20
+	Config       *runconfig.Config
19 21
 }
20 22
 
21 23
 // Commit creates a new filesystem image from the current state of a container.
22 24
 // The image can optionally be tagged into a repository.
23
-func (daemon *Daemon) Commit(container *Container, c *ContainerCommitConfig) (*image.Image, error) {
25
+func (daemon *Daemon) Commit(name string, c *ContainerCommitConfig) (*image.Image, error) {
26
+	container, err := daemon.Get(name)
27
+	if err != nil {
28
+		return nil, err
29
+	}
30
+
31
+	// It is not possible to commit a running container on Windows
32
+	if runtime.GOOS == "windows" && container.IsRunning() {
33
+		return nil, fmt.Errorf("Windows does not support commit of a running container")
34
+	}
35
+
24 36
 	if c.Pause && !container.isPaused() {
25 37
 		daemon.containerPause(container)
26 38
 		defer daemon.containerUnpause(container)
27 39
 	}
28 40
 
41
+	if c.MergeConfigs {
42
+		if err := runconfig.Merge(c.Config, container.Config); err != nil {
43
+			return nil, err
44
+		}
45
+	}
46
+
29 47
 	rwTar, err := daemon.exportContainerRw(container)
30 48
 	if err != nil {
31 49
 		return nil, err
... ...
@@ -106,8 +106,8 @@ func (d Docker) Remove(id string, cfg *daemon.ContainerRmConfig) error {
106 106
 }
107 107
 
108 108
 // Commit creates a new Docker image from an existing Docker container.
109
-func (d Docker) Commit(c *daemon.Container, cfg *daemon.ContainerCommitConfig) (*image.Image, error) {
110
-	return d.Daemon.Commit(c, cfg)
109
+func (d Docker) Commit(name string, cfg *daemon.ContainerCommitConfig) (*image.Image, error) {
110
+	return d.Daemon.Commit(name, cfg)
111 111
 }
112 112
 
113 113
 // Retain retains an image avoiding it to be removed or overwritten until a corresponding Release() call.