Skip to content

Commit 50de9fd

Browse files
committed
Make error message consistent when removing containers fail.
Signed-off-by: David Calavera <david.calavera@gmail.com>
1 parent 69c381c commit 50de9fd

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

api/client/rm.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,8 @@ func (cli *DockerCli) CmdRm(args ...string) error {
2828
}
2929
name = strings.Trim(name, "/")
3030

31-
options := types.ContainerRemoveOptions{
32-
ContainerID: name,
33-
RemoveVolumes: *v,
34-
RemoveLinks: *link,
35-
Force: *force,
36-
}
37-
38-
if err := cli.client.ContainerRemove(options); err != nil {
39-
errs = append(errs, fmt.Sprintf("Failed to remove container (%s): %s", name, err))
31+
if err := cli.removeContainer(name, *v, *link, *force); err != nil {
32+
errs = append(errs, err.Error())
4033
} else {
4134
fmt.Fprintf(cli.out, "%s\n", name)
4235
}
@@ -46,3 +39,16 @@ func (cli *DockerCli) CmdRm(args ...string) error {
4639
}
4740
return nil
4841
}
42+
43+
func (cli *DockerCli) removeContainer(containerID string, removeVolumes, removeLinks, force bool) error {
44+
options := types.ContainerRemoveOptions{
45+
ContainerID: containerID,
46+
RemoveVolumes: removeVolumes,
47+
RemoveLinks: removeLinks,
48+
Force: force,
49+
}
50+
if err := cli.client.ContainerRemove(options); err != nil {
51+
return fmt.Errorf("Failed to remove container (%s): %v", containerID, err)
52+
}
53+
return nil
54+
}

api/client/run.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -218,17 +218,13 @@ func (cli *DockerCli) CmdRun(args ...string) error {
218218
})
219219
}
220220

221-
defer func() {
222-
if *flAutoRemove {
223-
options := types.ContainerRemoveOptions{
224-
ContainerID: createResponse.ID,
225-
RemoveVolumes: true,
226-
}
227-
if err := cli.client.ContainerRemove(options); err != nil {
228-
fmt.Fprintf(cli.err, "Error deleting container: %s\n", err)
221+
if *flAutoRemove {
222+
defer func() {
223+
if err := cli.removeContainer(createResponse.ID, true, false, false); err != nil {
224+
fmt.Fprintf(cli.err, "%v\n", err)
229225
}
230-
}
231-
}()
226+
}()
227+
}
232228

233229
//start the container
234230
if err := cli.client.ContainerStart(createResponse.ID); err != nil {

0 commit comments

Comments
 (0)