From 6842c32d03b5a48fa066efe1c00c3a01a903354b Mon Sep 17 00:00:00 2001 From: ryane Date: Tue, 27 Oct 2015 19:53:49 -0400 Subject: [PATCH] add basic runtime constraints to docker_container --- .../docker/resource_docker_container.go | 18 ++++++++++++++ .../docker/resource_docker_container_funcs.go | 24 +++++++++++++++++++ .../docker/resource_docker_container_test.go | 14 +++++++++++ .../docker/r/container.html.markdown | 4 ++++ 4 files changed, 60 insertions(+) diff --git a/builtin/providers/docker/resource_docker_container.go b/builtin/providers/docker/resource_docker_container.go index 7d2fa34cd..48eac9a4d 100644 --- a/builtin/providers/docker/resource_docker_container.go +++ b/builtin/providers/docker/resource_docker_container.go @@ -171,6 +171,24 @@ func resourceDockerContainer() *schema.Resource { Optional: true, ForceNew: true, }, + + "memory": &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + ForceNew: true, + }, + + "memory_swap": &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + ForceNew: true, + }, + + "cpu_shares": &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + ForceNew: true, + }, }, } } diff --git a/builtin/providers/docker/resource_docker_container_funcs.go b/builtin/providers/docker/resource_docker_container_funcs.go index 800f0f8ab..0f1a9d9e0 100644 --- a/builtin/providers/docker/resource_docker_container_funcs.go +++ b/builtin/providers/docker/resource_docker_container_funcs.go @@ -120,6 +120,30 @@ func resourceDockerContainerCreate(d *schema.ResourceData, meta interface{}) err hostConfig.Links = stringSetToStringSlice(v.(*schema.Set)) } + if v, ok := d.GetOk("memory"); ok { + memory := int64(v.(int)) + if memory > 0 { + hostConfig.Memory = memory * 1024 * 1024 + } + } + + if v, ok := d.GetOk("memory_swap"); ok { + swap := int64(v.(int)) + if swap != 0 { + if swap > 0 { // only convert positive #s to bytes + swap = swap * 1024 * 1024 + } + hostConfig.MemorySwap = swap + } + } + + if v, ok := d.GetOk("cpu_shares"); ok { + shares := int64(v.(int)) + if shares > 0 { + hostConfig.CPUShares = shares + } + } + creationTime = time.Now() if err := client.StartContainer(retContainer.ID, hostConfig); err != nil { return fmt.Errorf("Unable to start container: %s", err) diff --git a/builtin/providers/docker/resource_docker_container_test.go b/builtin/providers/docker/resource_docker_container_test.go index 0d0fe734f..1402f129f 100644 --- a/builtin/providers/docker/resource_docker_container_test.go +++ b/builtin/providers/docker/resource_docker_container_test.go @@ -44,6 +44,17 @@ func TestAccDockerContainer_customized(t *testing.T) { return fmt.Errorf("Container has wrong restart policy: %s", c.HostConfig.RestartPolicy.Name) } + if c.HostConfig.Memory != (128 * 1024 * 1024) { + return fmt.Errorf("Container has wrong memory setting: %d", c.HostConfig.Memory) + } + + if c.HostConfig.MemorySwap != (128 * 1024 * 1024) { + return fmt.Errorf("Container has wrong memory swap setting: %d", c.HostConfig.Memory) + } + + if c.HostConfig.CPUShares != 512 { + return fmt.Errorf("Container has wrong cpu shares setting: %d", c.HostConfig.CPUShares) + } return nil } @@ -115,5 +126,8 @@ resource "docker_container" "foo" { entrypoint = ["/bin/bash", "-c", "ping localhost"] restart = "on-failure" max_retry_count = 5 + memory = 128 + memory_swap = 128 + cpu_shares = 512 } ` diff --git a/website/source/docs/providers/docker/r/container.html.markdown b/website/source/docs/providers/docker/r/container.html.markdown index 91c5a8659..c1a728f1a 100644 --- a/website/source/docs/providers/docker/r/container.html.markdown +++ b/website/source/docs/providers/docker/r/container.html.markdown @@ -59,6 +59,10 @@ The following arguments are supported: * `privileged` - (Optional, bool) Run container in privileged mode. * `publish_all_ports` - (Optional, bool) Publish all ports of the container. * `volumes` - (Optional) See [Volumes](#volumes) below for details. +* `memory` - (Optional, int) The memory limit for the container in MBs. +* `memory_swap` - (Optional, int) The total memory limit (memory + swap) for the + container in MBs. +* `cpu_shares` - (Optional, int) CPU shares (relative weight) for the container. ## Ports