add basic runtime constraints to docker_container

This commit is contained in:
ryane 2015-10-27 19:53:49 -04:00
parent 17d185808e
commit 6842c32d03
4 changed files with 60 additions and 0 deletions

View File

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

View File

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

View File

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

View File

@ -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.
<a id="ports"></a>
## Ports