feat: Add gpu flag to docker_container resource (#405)

* feat: Add gpu flag do docker_container resource.

* docs: Enhance gpus flag documentation.
This commit is contained in:
Martin 2022-07-15 11:15:28 +02:00 committed by GitHub
parent efd73ce9ff
commit 100db97ffa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 0 deletions

View file

@ -973,6 +973,12 @@ func resourceDockerContainer() *schema.Resource {
Optional: true,
ForceNew: true,
},
"gpus": {
Type: schema.TypeString,
Description: "GPU devices to add to the container. Currently, only the value `all` is supported. Passing any other value will result in unexpected behavior.",
Optional: true,
ForceNew: true,
},
},
}
}

View file

@ -15,6 +15,7 @@ import (
"strings"
"time"
"github.com/docker/cli/opts"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
@ -347,6 +348,18 @@ func resourceDockerContainerCreate(ctx context.Context, d *schema.ResourceData,
if v, ok := d.GetOk("group_add"); ok {
hostConfig.GroupAdd = stringSetToStringSlice(v.(*schema.Set))
}
if v, ok := d.GetOk("gpus"); ok {
if client.ClientVersion() >= "1.40" {
var gpu opts.GpuOpts
err := gpu.Set(v.(string))
if err != nil {
return diag.Errorf("Error setting gpus: %s", err)
}
hostConfig.DeviceRequests = gpu.Value()
} else {
log.Printf("[WARN] GPU support requires docker version 1.40 or higher")
}
}
init := d.Get("init").(bool)
hostConfig.Init = &init
@ -730,6 +743,13 @@ func resourceDockerContainerRead(ctx context.Context, d *schema.ResourceData, me
d.Set("stop_signal", container.Config.StopSignal)
d.Set("stop_timeout", container.Config.StopTimeout)
if len(container.HostConfig.DeviceRequests) > 0 {
// TODO pass the original gpus property string back to the resource
// var gpuOpts opts.GpuOpts
// gpuOpts = opts.GpuOpts{container.HostConfig.DeviceRequests}
d.Set("gpus", "all")
}
return nil
}