From 6fd5b02a0d1c8459cdbff5d3d211f2d7c49c167b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Este-Gracias?= Date: Mon, 11 Jul 2022 12:27:47 +0200 Subject: [PATCH] feat: add runtime, stop_signal and stop_timeout properties to the docker_container resource (#364) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: add property runtime to docker_container Signed-off-by: Stéphane Este-Gracias * feat: add properties stop_signal & stop_timeout Signed-off-by: Stéphane Este-Gracias * fix: stop_timeout cast Signed-off-by: Stéphane Este-Gracias * fix: add Computed to avoid recreation Signed-off-by: Stéphane Este-Gracias --- docs/resources/container.md | 3 +++ .../provider/resource_docker_container.go | 21 +++++++++++++++++++ .../resource_docker_container_funcs.go | 21 ++++++++++++++----- .../resource_docker_container_migrators.go | 21 +++++++++++++++++++ 4 files changed, 61 insertions(+), 5 deletions(-) diff --git a/docs/resources/container.md b/docs/resources/container.md index 895e614e..8f42db5b 100644 --- a/docs/resources/container.md +++ b/docs/resources/container.md @@ -76,10 +76,13 @@ resource "docker_image" "ubuntu" { - `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 when it exits. Defaults to `false`. +- `runtime` (String) Runtime to use for the container. - `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`. - `stdin_open` (Boolean) If `true`, keep STDIN open even if not attached (`docker run -i`). Defaults to `false`. +- `stop_signal` (String) Signal to stop a container (default `SIGTERM`). +- `stop_timeout` (Number) Timeout (in seconds) to stop a container. - `storage_opts` (Map of String) Key/value pairs for the storage driver options, e.g. `size`: `120G` - `sysctls` (Map of String) A map of kernel parameters (sysctls) to set in the container. - `tmpfs` (Map of String) A map of container directories which should be replaced by `tmpfs mounts`, and their corresponding mount options. diff --git a/internal/provider/resource_docker_container.go b/internal/provider/resource_docker_container.go index 08106d32..effdc507 100644 --- a/internal/provider/resource_docker_container.go +++ b/internal/provider/resource_docker_container.go @@ -274,6 +274,27 @@ func resourceDockerContainer() *schema.Resource { Elem: &schema.Schema{Type: schema.TypeString}, Set: schema.HashString, }, + "runtime": { + Type: schema.TypeString, + Description: "Runtime to use for the container.", + Optional: true, + ForceNew: true, + Computed: true, + }, + "stop_signal": { + Type: schema.TypeString, + Description: "Signal to stop a container (default `SIGTERM`).", + Optional: true, + ForceNew: true, + Computed: true, + }, + "stop_timeout": { + Type: schema.TypeInt, + Description: "Timeout (in seconds) to stop a container.", + Optional: true, + ForceNew: true, + Computed: true, + }, "mounts": { Type: schema.TypeSet, Description: "Specification for mounts to be added to containers created as part of the service.", diff --git a/internal/provider/resource_docker_container_funcs.go b/internal/provider/resource_docker_container_funcs.go index 81ae6c6e..988becbd 100644 --- a/internal/provider/resource_docker_container_funcs.go +++ b/internal/provider/resource_docker_container_funcs.go @@ -53,13 +53,20 @@ func resourceDockerContainerCreate(ctx context.Context, d *schema.ResourceData, if err != nil { return diag.Errorf("Unable to create container with image %s: %s", image, err) } + var stopTimeout *int + if v, ok := d.GetOk("stop_timeout"); ok { + tmp := v.(int) + stopTimeout = &tmp + } config := &container.Config{ - Image: image, - Hostname: d.Get("hostname").(string), - Domainname: d.Get("domainname").(string), - Tty: d.Get("tty").(bool), - OpenStdin: d.Get("stdin_open").(bool), + Image: image, + Hostname: d.Get("hostname").(string), + Domainname: d.Get("domainname").(string), + Tty: d.Get("tty").(bool), + OpenStdin: d.Get("stdin_open").(bool), + StopSignal: d.Get("stop_signal").(string), + StopTimeout: stopTimeout, } if v, ok := d.GetOk("env"); ok { @@ -229,6 +236,7 @@ func resourceDockerContainerCreate(ctx context.Context, d *schema.ResourceData, Name: d.Get("restart").(string), MaximumRetryCount: d.Get("max_retry_count").(int), }, + Runtime: d.Get("runtime").(string), Mounts: mounts, AutoRemove: d.Get("rm").(bool), ReadonlyRootfs: d.Get("read_only").(bool), @@ -660,6 +668,7 @@ func resourceDockerContainerRead(ctx context.Context, d *schema.ResourceData, me }, }) } + d.Set("runtime", container.HostConfig.Runtime) d.Set("mounts", getDockerContainerMounts(container)) // volumes d.Set("tmpfs", container.HostConfig.Tmpfs) @@ -718,6 +727,8 @@ func resourceDockerContainerRead(ctx context.Context, d *schema.ResourceData, me d.Set("group_add", container.HostConfig.GroupAdd) d.Set("tty", container.Config.Tty) d.Set("stdin_open", container.Config.OpenStdin) + d.Set("stop_signal", container.Config.StopSignal) + d.Set("stop_timeout", container.Config.StopTimeout) return nil } diff --git a/internal/provider/resource_docker_container_migrators.go b/internal/provider/resource_docker_container_migrators.go index b92e28f8..f99d25a6 100644 --- a/internal/provider/resource_docker_container_migrators.go +++ b/internal/provider/resource_docker_container_migrators.go @@ -219,6 +219,27 @@ func resourceDockerContainerV1() *schema.Resource { }, }, }, + "runtime": { + Type: schema.TypeString, + Description: "Runtime to use for the container.", + Optional: true, + ForceNew: true, + Computed: true, + }, + "stop_signal": { + Type: schema.TypeString, + Description: "Signal to stop a container (default `SIGTERM`).", + Optional: true, + ForceNew: true, + Computed: true, + }, + "stop_timeout": { + Type: schema.TypeInt, + Description: "Timeout (in seconds) to stop a container.", + Optional: true, + ForceNew: true, + Computed: true, + }, "mounts": { Type: schema.TypeSet, Description: "Specification for mounts to be added to containers created as part of the service",