feat: add runtime, stop_signal and stop_timeout properties to the docker_container resource (#364)

* feat: add property runtime to docker_container

Signed-off-by: Stéphane Este-Gracias <sestegra@gmail.com>

* feat: add properties stop_signal & stop_timeout

Signed-off-by: Stéphane Este-Gracias <sestegra@gmail.com>

* fix: stop_timeout cast

Signed-off-by: Stéphane Este-Gracias <sestegra@gmail.com>

* fix: add Computed to avoid recreation

Signed-off-by: Stéphane Este-Gracias <sestegra@gmail.com>
This commit is contained in:
Stéphane Este-Gracias 2022-07-11 12:27:47 +02:00 committed by GitHub
parent af072b22aa
commit 6fd5b02a0d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 5 deletions

View file

@ -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.

View file

@ -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.",

View file

@ -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
}

View file

@ -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",