Browse code

'docker commit' can optionally tag the new image into a repository

Solomon Hykes authored on 2013/03/22 12:07:37
Showing 2 changed files
... ...
@@ -531,33 +531,22 @@ func (srv *Server) CmdPs(stdin io.ReadCloser, stdout io.Writer, args ...string)
531 531
 
532 532
 func (srv *Server) CmdCommit(stdin io.ReadCloser, stdout io.Writer, args ...string) error {
533 533
 	cmd := rcli.Subcmd(stdout,
534
-		"commit", "[OPTIONS] CONTAINER [DEST]",
534
+		"commit", "[OPTIONS] CONTAINER [REPOSITORY [TAG]]",
535 535
 		"Create a new image from a container's changes")
536 536
 	if err := cmd.Parse(args); err != nil {
537 537
 		return nil
538 538
 	}
539
-	containerName, imgName := cmd.Arg(0), cmd.Arg(1)
540
-	if containerName == "" || imgName == "" {
539
+	containerName, repository, tag := cmd.Arg(0), cmd.Arg(1), cmd.Arg(2)
540
+	if containerName == "" {
541 541
 		cmd.Usage()
542 542
 		return nil
543 543
 	}
544
-	if container := srv.runtime.Get(containerName); container != nil {
545
-		// FIXME: freeze the container before copying it to avoid data corruption?
546
-		// FIXME: this shouldn't be in commands.
547
-		rwTar, err := container.ExportRw()
548
-		if err != nil {
549
-			return err
550
-		}
551
-		// Create a new image from the container's base layers + a new layer from container changes
552
-		img, err := srv.runtime.graph.Create(rwTar, container.Image, "")
553
-		if err != nil {
554
-			return err
555
-		}
556
-
557
-		fmt.Fprintln(stdout, img.Id)
558
-		return nil
544
+	img, err := srv.runtime.Commit(containerName, repository, tag)
545
+	if err != nil {
546
+		return err
559 547
 	}
560
-	return errors.New("No such container: " + containerName)
548
+	fmt.Fprintln(stdout, img.Id)
549
+	return nil
561 550
 }
562 551
 
563 552
 func (srv *Server) CmdExport(stdin io.ReadCloser, stdout io.Writer, args ...string) error {
... ...
@@ -200,6 +200,33 @@ func (runtime *Runtime) Destroy(container *Container) error {
200 200
 	return nil
201 201
 }
202 202
 
203
+// Commit creates a new filesystem image from the current state of a container.
204
+// The image can optionally be tagged into a repository
205
+func (runtime *Runtime) Commit(id, repository, tag string) (*Image, error) {
206
+	container := runtime.Get(id)
207
+	if container == nil {
208
+		return nil, fmt.Errorf("No such container: %s", id)
209
+	}
210
+	// FIXME: freeze the container before copying it to avoid data corruption?
211
+	// FIXME: this shouldn't be in commands.
212
+	rwTar, err := container.ExportRw()
213
+	if err != nil {
214
+		return nil, err
215
+	}
216
+	// Create a new image from the container's base layers + a new layer from container changes
217
+	img, err := runtime.graph.Create(rwTar, container.Image, "")
218
+	if err != nil {
219
+		return nil, err
220
+	}
221
+	// Register the image if needed
222
+	if repository != "" {
223
+		if err := runtime.repositories.Set(repository, tag, img.Id); err != nil {
224
+			return img, err
225
+		}
226
+	}
227
+	return img, nil
228
+}
229
+
203 230
 func (runtime *Runtime) restore() error {
204 231
 	dir, err := ioutil.ReadDir(runtime.repository)
205 232
 	if err != nil {