fix: pass container rm flag (#322)

* fix: pass container rm flag

* fix: deletion of containers

* docs: website gen

* docs: add stargazers

* fix(docs): for #321
This commit is contained in:
Manuel Vogel 2021-12-03 11:38:55 +01:00 committed by GitHub
parent 0da4706851
commit 3fbe7d3a34
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 35 additions and 19 deletions

View file

@ -109,3 +109,8 @@ To contribute, please read the contribution guidelines: [Contributing to Terrafo
## License
The Terraform Provider Docker is available to everyone under the terms of the Mozilla Public License Version 2.0. [Take a look the LICENSE file](LICENSE).
## Stargazers over time
[![Stargazers over time](https://starchart.cc/kreuzwerker/terraform-provider-docker.svg)](https://starchart.cc/kreuzwerker/terraform-provider-docker)

View file

@ -76,7 +76,7 @@ resource "docker_image" "ubuntu" {
- **read_only** (Boolean) If `true`, the container will be started as readonly. Defaults to `false`.
- **remove_volumes** (Boolean) If `true`, it will remove anonymous volumes associated with the container. Defaults to `true`.
- **restart** (String) The restart policy for the container. Must be one of 'no', 'on-failure', 'always', 'unless-stopped'. Defaults to `no`.
- **rm** (Boolean) If `true`, then the container will be automatically removed after his execution. Terraform won't check this container after creation. Defaults to `false`.
- **rm** (Boolean) If `true`, then the container will be automatically removed when it exits. Defaults to `false`.
- **security_opts** (Set of String) List of string values to customize labels for MLS systems, such as SELinux. See https://docs.docker.com/engine/reference/run/#security-configuration.
- **shm_size** (Number) Size of `/dev/shm` in MBs.
- **start** (Boolean) If `true`, then the Docker container will be started after creation. If `false`, then the container is only created. Defaults to `true`.

View file

@ -3,13 +3,12 @@
page_title: "docker_registry_image Resource - terraform-provider-docker"
subcategory: ""
description: |-
Manages the lifecycle of docker image/tag in a registry.
Manages the lifecycle of docker image/tag in a registry means it can store one or more version of specific docker images and identified by their tags.
---
<!-- Bug: Type and Name are switched -->
# docker_registry_image (Resource)
Manages the lifecycle of docker image/tag in a registry means it can store one or more version
of specific docker images and identified by their tags.
Manages the lifecycle of docker image/tag in a registry means it can store one or more version of specific docker images and identified by their tags.
## Example Usage

View file

@ -44,7 +44,7 @@ func resourceDockerContainer() *schema.Resource {
"rm": {
Type: schema.TypeBool,
Description: "If `true`, then the container will be automatically removed after his execution. Terraform won't check this container after creation. Defaults to `false`.",
Description: "If `true`, then the container will be automatically removed when it exits. Defaults to `false`.",
Default: false,
Optional: true,
},

View file

@ -824,32 +824,39 @@ func resourceDockerContainerUpdate(ctx context.Context, d *schema.ResourceData,
func resourceDockerContainerDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*ProviderConfig).DockerClient
if d.Get("rm").(bool) {
d.SetId("")
return nil
}
if !d.Get("attach").(bool) {
// Stop the container before removing if destroy_grace_seconds is defined
var timeout time.Duration
if d.Get("destroy_grace_seconds").(int) > 0 {
timeout := time.Duration(int32(d.Get("destroy_grace_seconds").(int))) * time.Second
timeout = time.Duration(int32(d.Get("destroy_grace_seconds").(int))) * time.Second
}
log.Printf("[INFO] Stopping Container '%s' with timeout %v", d.Id(), timeout)
if err := client.ContainerStop(ctx, d.Id(), &timeout); err != nil {
return diag.Errorf("Error stopping container %s: %s", d.Id(), err)
}
}
}
removeOpts := types.ContainerRemoveOptions{
RemoveVolumes: d.Get("remove_volumes").(bool),
RemoveLinks: d.Get("rm").(bool),
Force: true,
}
log.Printf("[INFO] Removing Container '%s'", d.Id())
if err := client.ContainerRemove(ctx, d.Id(), removeOpts); err != nil {
if !containsIgnorableErrorMessage(err.Error(), "No such container", "is already in progress") {
return diag.Errorf("Error deleting container %s: %s", d.Id(), err)
}
}
waitOkC, errorC := client.ContainerWait(ctx, d.Id(), container.WaitConditionRemoved)
waitCondition := container.WaitConditionNotRunning
if d.Get("rm").(bool) {
waitCondition = container.WaitConditionRemoved
}
log.Printf("[INFO] Waiting for Container '%s' with condition '%s'", d.Id(), waitCondition)
waitOkC, errorC := client.ContainerWait(ctx, d.Id(), waitCondition)
select {
case waitOk := <-waitOkC:
log.Printf("[INFO] Container exited with code [%v]: '%s'", waitOk.StatusCode, d.Id())
@ -857,6 +864,7 @@ func resourceDockerContainerDelete(ctx context.Context, d *schema.ResourceData,
if !containsIgnorableErrorMessage(err.Error(), "No such container", "is already in progress") {
return diag.Errorf("Error waiting for container removal '%s': %s", d.Id(), err)
}
log.Printf("[INFO] Waiting for Container '%s' errord: '%s'", d.Id(), err.Error())
}
d.SetId("")

View file

@ -1730,7 +1730,9 @@ func testAccContainerWaitConditionRemoved(ctx context.Context, n string, ct *typ
select {
case err := <-errC:
if err != nil {
return fmt.Errorf("Container has not been removed")
if !containsIgnorableErrorMessage(err.Error(), "No such container", "is already in progress") {
return fmt.Errorf("Container has not been removed: '%s'", err.Error())
}
}
case <-statusC:
}

View file

@ -8,7 +8,7 @@ import (
func resourceDockerRegistryImage() *schema.Resource {
return &schema.Resource{
Description: "Manages the lifecycle of docker image/tag in a registry.",
Description: "Manages the lifecycle of docker image/tag in a registry means it can store one or more version of specific docker images and identified by their tags.",
CreateContext: resourceDockerRegistryImageCreate,
ReadContext: resourceDockerRegistryImageRead,

View file

@ -12,6 +12,8 @@ description: |-
## Example Usage
To be able to update an image itself when an updated image arrives.
{{tffile "examples/resources/docker_registry_image/resource.tf"}}
{{ .SchemaMarkdown | trimspace }}