Stop/Kill options for containers removal

Docker-DCO-1.1-Signed-off-by: Adrien Folie <folie.adrien@gmail.com> (github: folieadrien)
This commit is contained in:
Adrien Folie 2014-07-06 22:43:24 +02:00 committed by Victor Vieux
parent d53db040c8
commit 7810070630
3 changed files with 29 additions and 7 deletions

View file

@ -983,7 +983,8 @@ func (cli *DockerCli) CmdRm(args ...string) error {
cmd := cli.Subcmd("rm", "[OPTIONS] CONTAINER [CONTAINER...]", "Remove one or more containers")
v := cmd.Bool([]string{"v", "-volumes"}, false, "Remove the volumes associated with the container")
link := cmd.Bool([]string{"l", "#link", "-link"}, false, "Remove the specified link and not the underlying container")
force := cmd.Bool([]string{"f", "-force"}, false, "Force removal of running container")
stop := cmd.Bool([]string{"#f", "s", "#-force", "-stop"}, false, "Stop and remove a running container")
kill := cmd.Bool([]string{"k", "-kill"}, false, "Kill and remove a running container")
if err := cmd.Parse(args); err != nil {
return nil
@ -992,6 +993,9 @@ func (cli *DockerCli) CmdRm(args ...string) error {
cmd.Usage()
return nil
}
if *stop && *kill {
return fmt.Errorf("Conflicting options: -s/--stop and -k/--kill")
}
val := url.Values{}
if *v {
val.Set("v", "1")
@ -999,8 +1003,11 @@ func (cli *DockerCli) CmdRm(args ...string) error {
if *link {
val.Set("link", "1")
}
if *force {
val.Set("force", "1")
if *stop {
val.Set("stop", "1")
}
if *kill {
val.Set("kill", "1")
}
var encounteredError error

View file

@ -678,9 +678,19 @@ func deleteContainers(eng *engine.Engine, version version.Version, w http.Respon
return fmt.Errorf("Missing parameter")
}
job := eng.Job("container_delete", vars["name"])
if version.GreaterThanOrEqualTo("1.14") {
job.Setenv("stop", r.Form.Get("stop"))
job.Setenv("kill", r.Form.Get("kill"))
if job.GetenvBool("stop") && job.GetenvBool("kill") {
return fmt.Errorf("Bad parameters: can't use stop and kill simultaneously")
}
} else {
job.Setenv("stop", r.Form.Get("force"))
}
job.Setenv("removeVolume", r.Form.Get("v"))
job.Setenv("removeLink", r.Form.Get("link"))
job.Setenv("forceRemove", r.Form.Get("force"))
if err := job.Run(); err != nil {
return err
}

View file

@ -1787,7 +1787,8 @@ func (srv *Server) ContainerDestroy(job *engine.Job) engine.Status {
name := job.Args[0]
removeVolume := job.GetenvBool("removeVolume")
removeLink := job.GetenvBool("removeLink")
forceRemove := job.GetenvBool("forceRemove")
stop := job.GetenvBool("stop")
kill := job.GetenvBool("kill")
container := srv.daemon.Get(name)
@ -1821,12 +1822,16 @@ func (srv *Server) ContainerDestroy(job *engine.Job) engine.Status {
if container != nil {
if container.State.IsRunning() {
if forceRemove {
if stop {
if err := container.Stop(5); err != nil {
return job.Errorf("Could not stop running container, cannot remove - %v", err)
}
} else if kill {
if err := container.Kill(); err != nil {
return job.Errorf("Could not kill running container, cannot remove - %v", err)
}
} else {
return job.Errorf("Impossible to remove a running container, please stop it first or use -f")
return job.Errorf("You cannot remove a running container. Stop the container before attempting removal or use -s or -k")
}
}
if err := srv.daemon.Destroy(container); err != nil {