Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support setting cpu shares #575

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/resources/container.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ resource "docker_image" "ubuntu" {
- `domainname` (String) Domain name of the container.
- `entrypoint` (List of String) The command to use as the Entrypoint for the container. The Entrypoint allows you to configure a container to run as an executable. For example, to run `/usr/bin/myprogram` when starting a container, set the entrypoint to be `"/usr/bin/myprogra"]`.
- `env` (Set of String) Environment variables to set in the form of `KEY=VALUE`, e.g. `DEBUG=0`
- `cpus` (String) Specify how much of the available CPU resources a container can use. e.g a value of 1.5 means the container is guaranteed at most one and a half of the CPUs
- `gpus` (String) GPU devices to add to the container. Currently, only the value `all` is supported. Passing any other value will result in unexpected behavior.
- `group_add` (Set of String) Additional groups for the container user
- `healthcheck` (Block List, Max: 1) A test to perform to check that the container is healthy (see [below for nested schema](#nestedblock--healthcheck))
Expand Down
6 changes: 6 additions & 0 deletions internal/provider/resource_docker_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,12 @@ func resourceDockerContainer() *schema.Resource {
Optional: true,
ForceNew: true,
},
"cpus": {
Type: schema.TypeString,
Description: "Specify how much of the available CPU resources a container can use. e.g a value of 1.5 means the container is guaranteed at most one and a half of the CPUs",
Optional: true,
ForceNew: true,
},
"cgroupns_mode": {
Type: schema.TypeString,
Description: "Cgroup namespace mode to use for the container. Possible values are: `private`, `host`.",
Expand Down
21 changes: 21 additions & 0 deletions internal/provider/resource_docker_container_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"errors"
"fmt"
"log"
"math/big"
"os"
"strings"
"time"
Expand Down Expand Up @@ -311,6 +312,23 @@ func resourceDockerContainerCreate(ctx context.Context, d *schema.ResourceData,
hostConfig.ShmSize = int64(v.(int)) * 1024 * 1024
}

if v, ok := d.GetOk("cpus"); ok {
if client.ClientVersion() >= "1.28" {
cpu, ok := new(big.Rat).SetString(v.(string))
if !ok {
return diag.Errorf("Error setting cpus: Failed to parse %v as a rational number", v.(string))
}
nano := cpu.Mul(cpu, big.NewRat(1e9, 1))
if !nano.IsInt() {
return diag.Errorf("Error setting cpus: value is too precise")
}

hostConfig.NanoCPUs = nano.Num().Int64()
} else {
log.Printf("[WARN] Setting CPUs count/quota requires docker version 1.28 or higher")
}
}

if v, ok := d.GetOk("cpu_shares"); ok {
hostConfig.CPUShares = int64(v.(int))
}
Expand Down Expand Up @@ -726,6 +744,9 @@ func resourceDockerContainerRead(ctx context.Context, d *schema.ResourceData, me
d.Set("memory_swap", container.HostConfig.MemorySwap)
}
d.Set("shm_size", container.HostConfig.ShmSize/1024/1024)
if container.HostConfig.NanoCPUs > 0 {
d.Set("cpus", container.HostConfig.NanoCPUs)
}
d.Set("cpu_shares", container.HostConfig.CPUShares)
d.Set("cpu_set", container.HostConfig.CpusetCpus)
d.Set("log_driver", container.HostConfig.LogConfig.Type)
Expand Down