Merge pull request #11570 from runcom/9658-consistent-autoremove

Remove container if --rm flag is passed and container cannot be started
This commit is contained in:
Alexander Morozov 2015-03-23 15:38:51 -07:00
commit 197a3f0a98
2 changed files with 51 additions and 3 deletions

View file

@ -2495,6 +2495,14 @@ func (cli *DockerCli) CmdRun(args ...string) error {
}
}
defer func() {
if *flAutoRemove {
if _, _, err = readBody(cli.call("DELETE", "/containers/"+createResponse.ID+"?v=1", nil, false)); err != nil {
log.Errorf("Error deleting container: %s", err)
}
}
}()
//start the container
if _, _, err = readBody(cli.call("POST", "/containers/"+createResponse.ID+"/start", nil, false)); err != nil {
return err
@ -2532,9 +2540,6 @@ func (cli *DockerCli) CmdRun(args ...string) error {
if _, status, err = getExitCode(cli, createResponse.ID); err != nil {
return err
}
if _, _, err := readBody(cli.call("DELETE", "/containers/"+createResponse.ID+"?v=1", nil, false)); err != nil {
return err
}
} else {
// No Autoremove: Simply retrieve the exit code
if !config.Tty {

View file

@ -3348,3 +3348,46 @@ func TestRunVolumesFromRestartAfterRemoved(t *testing.T) {
logDone("run - can restart a volumes-from container after producer is removed")
}
// run container with --rm should remove container if exit code != 0
func TestRunContainerWithRmFlagExitCodeNotEqualToZero(t *testing.T) {
defer deleteAllContainers()
runCmd := exec.Command(dockerBinary, "run", "--rm", "busybox", "ls", "/notexists")
out, _, err := runCommandWithOutput(runCmd)
if err == nil {
t.Fatal("Expected docker run to fail", out, err)
}
out, err = getAllContainers()
if err != nil {
t.Fatal(out, err)
}
if out != "" {
t.Fatal("Expected not to have containers", out)
}
logDone("run - container is removed if run with --rm and exit code != 0")
}
func TestRunContainerWithRmFlagCannotStartContainer(t *testing.T) {
defer deleteAllContainers()
runCmd := exec.Command(dockerBinary, "run", "--rm", "busybox", "commandNotFound")
out, _, err := runCommandWithOutput(runCmd)
if err == nil {
t.Fatal("Expected docker run to fail", out, err)
}
out, err = getAllContainers()
if err != nil {
t.Fatal(out, err)
}
if out != "" {
t.Fatal("Expected not to have containers", out)
}
logDone("run - container is removed if run with --rm and cannot start")
}