Browse Source

Merge pull request #19896 from calavera/same_rm_error_message

Make error message consistent when removing containers fail.
Brian Goff 9 năm trước cách đây
mục cha
commit
3c0c115ec8
2 tập tin đã thay đổi với 21 bổ sung19 xóa
  1. 15 9
      api/client/rm.go
  2. 6 10
      api/client/run.go

+ 15 - 9
api/client/rm.go

@@ -28,15 +28,8 @@ func (cli *DockerCli) CmdRm(args ...string) error {
 		}
 		name = strings.Trim(name, "/")
 
-		options := types.ContainerRemoveOptions{
-			ContainerID:   name,
-			RemoveVolumes: *v,
-			RemoveLinks:   *link,
-			Force:         *force,
-		}
-
-		if err := cli.client.ContainerRemove(options); err != nil {
-			errs = append(errs, fmt.Sprintf("Failed to remove container (%s): %s", name, err))
+		if err := cli.removeContainer(name, *v, *link, *force); err != nil {
+			errs = append(errs, err.Error())
 		} else {
 			fmt.Fprintf(cli.out, "%s\n", name)
 		}
@@ -46,3 +39,16 @@ func (cli *DockerCli) CmdRm(args ...string) error {
 	}
 	return nil
 }
+
+func (cli *DockerCli) removeContainer(containerID string, removeVolumes, removeLinks, force bool) error {
+	options := types.ContainerRemoveOptions{
+		ContainerID:   containerID,
+		RemoveVolumes: removeVolumes,
+		RemoveLinks:   removeLinks,
+		Force:         force,
+	}
+	if err := cli.client.ContainerRemove(options); err != nil {
+		return fmt.Errorf("Failed to remove container (%s): %v", containerID, err)
+	}
+	return nil
+}

+ 6 - 10
api/client/run.go

@@ -219,17 +219,13 @@ func (cli *DockerCli) CmdRun(args ...string) error {
 		})
 	}
 
-	defer func() {
-		if *flAutoRemove {
-			options := types.ContainerRemoveOptions{
-				ContainerID:   createResponse.ID,
-				RemoveVolumes: true,
-			}
-			if err := cli.client.ContainerRemove(options); err != nil {
-				fmt.Fprintf(cli.err, "Error deleting container: %s\n", err)
+	if *flAutoRemove {
+		defer func() {
+			if err := cli.removeContainer(createResponse.ID, true, false, false); err != nil {
+				fmt.Fprintf(cli.err, "%v\n", err)
 			}
-		}
-	}()
+		}()
+	}
 
 	//start the container
 	if err := cli.client.ContainerStart(createResponse.ID); err != nil {