Remove container if --rm flag is passed and container cannot be started
| ... | ... |
@@ -2495,6 +2495,14 @@ func (cli *DockerCli) CmdRun(args ...string) error {
|
| 2495 | 2495 |
} |
| 2496 | 2496 |
} |
| 2497 | 2497 |
|
| 2498 |
+ defer func() {
|
|
| 2499 |
+ if *flAutoRemove {
|
|
| 2500 |
+ if _, _, err = readBody(cli.call("DELETE", "/containers/"+createResponse.ID+"?v=1", nil, false)); err != nil {
|
|
| 2501 |
+ log.Errorf("Error deleting container: %s", err)
|
|
| 2502 |
+ } |
|
| 2503 |
+ } |
|
| 2504 |
+ }() |
|
| 2505 |
+ |
|
| 2498 | 2506 |
//start the container |
| 2499 | 2507 |
if _, _, err = readBody(cli.call("POST", "/containers/"+createResponse.ID+"/start", nil, false)); err != nil {
|
| 2500 | 2508 |
return err |
| ... | ... |
@@ -2532,9 +2540,6 @@ func (cli *DockerCli) CmdRun(args ...string) error {
|
| 2532 | 2532 |
if _, status, err = getExitCode(cli, createResponse.ID); err != nil {
|
| 2533 | 2533 |
return err |
| 2534 | 2534 |
} |
| 2535 |
- if _, _, err := readBody(cli.call("DELETE", "/containers/"+createResponse.ID+"?v=1", nil, false)); err != nil {
|
|
| 2536 |
- return err |
|
| 2537 |
- } |
|
| 2538 | 2535 |
} else {
|
| 2539 | 2536 |
// No Autoremove: Simply retrieve the exit code |
| 2540 | 2537 |
if !config.Tty {
|
| ... | ... |
@@ -3348,3 +3348,46 @@ func TestRunVolumesFromRestartAfterRemoved(t *testing.T) {
|
| 3348 | 3348 |
|
| 3349 | 3349 |
logDone("run - can restart a volumes-from container after producer is removed")
|
| 3350 | 3350 |
} |
| 3351 |
+ |
|
| 3352 |
+// run container with --rm should remove container if exit code != 0 |
|
| 3353 |
+func TestRunContainerWithRmFlagExitCodeNotEqualToZero(t *testing.T) {
|
|
| 3354 |
+ defer deleteAllContainers() |
|
| 3355 |
+ |
|
| 3356 |
+ runCmd := exec.Command(dockerBinary, "run", "--rm", "busybox", "ls", "/notexists") |
|
| 3357 |
+ out, _, err := runCommandWithOutput(runCmd) |
|
| 3358 |
+ if err == nil {
|
|
| 3359 |
+ t.Fatal("Expected docker run to fail", out, err)
|
|
| 3360 |
+ } |
|
| 3361 |
+ |
|
| 3362 |
+ out, err = getAllContainers() |
|
| 3363 |
+ if err != nil {
|
|
| 3364 |
+ t.Fatal(out, err) |
|
| 3365 |
+ } |
|
| 3366 |
+ |
|
| 3367 |
+ if out != "" {
|
|
| 3368 |
+ t.Fatal("Expected not to have containers", out)
|
|
| 3369 |
+ } |
|
| 3370 |
+ |
|
| 3371 |
+ logDone("run - container is removed if run with --rm and exit code != 0")
|
|
| 3372 |
+} |
|
| 3373 |
+ |
|
| 3374 |
+func TestRunContainerWithRmFlagCannotStartContainer(t *testing.T) {
|
|
| 3375 |
+ defer deleteAllContainers() |
|
| 3376 |
+ |
|
| 3377 |
+ runCmd := exec.Command(dockerBinary, "run", "--rm", "busybox", "commandNotFound") |
|
| 3378 |
+ out, _, err := runCommandWithOutput(runCmd) |
|
| 3379 |
+ if err == nil {
|
|
| 3380 |
+ t.Fatal("Expected docker run to fail", out, err)
|
|
| 3381 |
+ } |
|
| 3382 |
+ |
|
| 3383 |
+ out, err = getAllContainers() |
|
| 3384 |
+ if err != nil {
|
|
| 3385 |
+ t.Fatal(out, err) |
|
| 3386 |
+ } |
|
| 3387 |
+ |
|
| 3388 |
+ if out != "" {
|
|
| 3389 |
+ t.Fatal("Expected not to have containers", out)
|
|
| 3390 |
+ } |
|
| 3391 |
+ |
|
| 3392 |
+ logDone("run - container is removed if run with --rm and cannot start")
|
|
| 3393 |
+} |