Add `destroy_grace_seconds` option to stop container before delete (#7513)

This commit is contained in:
JB Arsenault 2016-07-11 11:03:02 -04:00 committed by Paul Stack
parent 9153956c73
commit fc838be69e
4 changed files with 15 additions and 0 deletions

View File

@ -285,6 +285,11 @@ func resourceDockerContainer() *schema.Resource {
ForceNew: true,
},
"destroy_grace_seconds": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
},
"labels": &schema.Schema{
Type: schema.TypeMap,
Optional: true,

View File

@ -265,6 +265,14 @@ func resourceDockerContainerUpdate(d *schema.ResourceData, meta interface{}) err
func resourceDockerContainerDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*dc.Client)
// Stop the container before removing if destroy_grace_seconds is defined
if d.Get("destroy_grace_seconds").(int) > 0 {
var timeout = uint(d.Get("destroy_grace_seconds").(int))
if err := client.StopContainer(d.Id(), timeout); err != nil {
return fmt.Errorf("Error stopping container %s: %s", d.Id(), err)
}
}
removeOpts := dc.RemoveContainerOptions{
ID: d.Id(),
RemoveVolumes: true,

View File

@ -255,6 +255,7 @@ resource "docker_container" "foo" {
entrypoint = ["/bin/bash", "-c", "ping localhost"]
user = "root:root"
restart = "on-failure"
destroy_grace_seconds = 10
max_retry_count = 5
memory = 512
memory_swap = 2048

View File

@ -79,6 +79,7 @@ The following arguments are supported:
* `network_mode` - (Optional, string) Network mode of the container.
* `networks` - (Optional, set of strings) Id of the networks in which the
container is.
* `destroy_grace_seconds` - (Optional, int) If defined will attempt to stop the container before destroying. Container will be destroyed after `n` seconds or on successful stop.
<a id="ports"></a>
### Ports