feat: supports to update docker_container (#236)

According to the docs
* https://docs.docker.com/engine/api/v1.40/#operation/ContainerUpdate
* https://godoc.org/github.com/docker/docker/client#Client.ContainerUpdate
This commit is contained in:
Shunsuke Suzuki 2020-02-02 14:39:28 +09:00 committed by GitHub
parent 33c17570c9
commit bed25bf419
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 67 additions and 9 deletions

View file

@ -179,7 +179,6 @@ func resourceDockerContainer() *schema.Resource {
"restart": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "no",
ValidateFunc: validateStringMatchesPattern(`^(no|on-failure|always|unless-stopped)$`),
},
@ -187,7 +186,6 @@ func resourceDockerContainer() *schema.Resource {
"max_retry_count": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
"working_dir": {
Type: schema.TypeString,
@ -568,14 +566,12 @@ func resourceDockerContainer() *schema.Resource {
"memory": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
ValidateFunc: validateIntegerGeqThan(0),
},
"memory_swap": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
ValidateFunc: validateIntegerGeqThan(-1),
},
@ -590,14 +586,12 @@ func resourceDockerContainer() *schema.Resource {
"cpu_shares": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
ValidateFunc: validateIntegerGeqThan(0),
},
"cpu_set": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validateStringMatchesPattern(`^\d+([,-]\d+)*$`),
},
@ -1338,7 +1332,6 @@ func resourceDockerContainerV1() *schema.Resource {
"memory": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
ValidateFunc: validateIntegerGeqThan(0),
},
@ -1366,7 +1359,6 @@ func resourceDockerContainerV1() *schema.Resource {
"cpu_set": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validateStringMatchesPattern(`^\d+([,-]\d+)*$`),
},

View file

@ -682,7 +682,49 @@ func resourceDockerContainerRead(d *schema.ResourceData, meta interface{}) error
}
func resourceDockerContainerUpdate(d *schema.ResourceData, meta interface{}) error {
// TODO call resourceDockerContainerRead here
attrs := []string{
"restart", "max_retry_count", "cpu_shares", "memory", "cpu_set", "memory_swap",
}
for _, attr := range attrs {
if d.HasChange(attr) {
// TODO update ulimits
// Updating ulimits seems not to work well.
// It succeeds to run `DockerClient.ContainerUpdate` with `ulimit` but actually `ulimit` aren't changed.
// https://github.com/terraform-providers/terraform-provider-docker/pull/236#discussion_r373819536
// ulimits := []*units.Ulimit{}
// if v, ok := d.GetOk("ulimit"); ok {
// ulimits = ulimitsToDockerUlimits(v.(*schema.Set))
// }
updateConfig := container.UpdateConfig{
RestartPolicy: container.RestartPolicy{
Name: d.Get("restart").(string),
MaximumRetryCount: d.Get("max_retry_count").(int),
},
Resources: container.Resources{
CPUShares: int64(d.Get("cpu_shares").(int)),
Memory: int64(d.Get("memory").(int)) * 1024 * 1024,
CpusetCpus: d.Get("cpu_set").(string),
// Ulimits: ulimits,
},
}
if ms, ok := d.GetOk("memory_swap"); ok {
a := int64(ms.(int))
if a > 0 {
a = a * 1024 * 1024
}
updateConfig.Resources.MemorySwap = a
}
client := meta.(*ProviderConfig).DockerClient
_, err := client.ContainerUpdate(context.Background(), d.Id(), updateConfig)
if err != nil {
return fmt.Errorf("Unable to update a container: %w", err)
}
break
}
}
return nil
}

View file

@ -66,6 +66,12 @@ func TestAccDockerContainer_basic(t *testing.T) {
testAccContainerRunning(resourceName, &c),
),
},
{
Config: testAccDockerContainerUpdateConfig,
Check: resource.ComposeTestCheckFunc(
testAccContainerRunning(resourceName, &c),
),
},
{
ResourceName: "docker_container.foo",
ImportState: true,
@ -1580,6 +1586,24 @@ resource "docker_container" "foo" {
}
`
const testAccDockerContainerUpdateConfig = `
resource "docker_image" "foo" {
name = "nginx:latest"
}
resource "docker_container" "foo" {
name = "tf-test"
image = "${docker_image.foo.latest}"
restart = "on-failure"
max_retry_count = 5
cpu_shares = 32
cpu_set = "0-1"
memory = 512
memory_swap = 2048
}
`
const testAccDockerContainerWith2BridgeNetworkConfig = `
resource "docker_network" "tftest" {
name = "tftest-contnw"