Skip to content

Commit bed25bf

Browse files
1 parent 33c1757 commit bed25bf

3 files changed

+67
-9
lines changed

docker/resource_docker_container.go

-8
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,13 @@ func resourceDockerContainer() *schema.Resource {
179179
"restart": {
180180
Type: schema.TypeString,
181181
Optional: true,
182-
ForceNew: true,
183182
Default: "no",
184183
ValidateFunc: validateStringMatchesPattern(`^(no|on-failure|always|unless-stopped)$`),
185184
},
186185

187186
"max_retry_count": {
188187
Type: schema.TypeInt,
189188
Optional: true,
190-
ForceNew: true,
191189
},
192190
"working_dir": {
193191
Type: schema.TypeString,
@@ -568,14 +566,12 @@ func resourceDockerContainer() *schema.Resource {
568566
"memory": {
569567
Type: schema.TypeInt,
570568
Optional: true,
571-
ForceNew: true,
572569
ValidateFunc: validateIntegerGeqThan(0),
573570
},
574571

575572
"memory_swap": {
576573
Type: schema.TypeInt,
577574
Optional: true,
578-
ForceNew: true,
579575
ValidateFunc: validateIntegerGeqThan(-1),
580576
},
581577

@@ -590,14 +586,12 @@ func resourceDockerContainer() *schema.Resource {
590586
"cpu_shares": {
591587
Type: schema.TypeInt,
592588
Optional: true,
593-
ForceNew: true,
594589
ValidateFunc: validateIntegerGeqThan(0),
595590
},
596591

597592
"cpu_set": {
598593
Type: schema.TypeString,
599594
Optional: true,
600-
ForceNew: true,
601595
ValidateFunc: validateStringMatchesPattern(`^\d+([,-]\d+)*$`),
602596
},
603597

@@ -1338,7 +1332,6 @@ func resourceDockerContainerV1() *schema.Resource {
13381332
"memory": {
13391333
Type: schema.TypeInt,
13401334
Optional: true,
1341-
ForceNew: true,
13421335
ValidateFunc: validateIntegerGeqThan(0),
13431336
},
13441337

@@ -1366,7 +1359,6 @@ func resourceDockerContainerV1() *schema.Resource {
13661359
"cpu_set": {
13671360
Type: schema.TypeString,
13681361
Optional: true,
1369-
ForceNew: true,
13701362
ValidateFunc: validateStringMatchesPattern(`^\d+([,-]\d+)*$`),
13711363
},
13721364

docker/resource_docker_container_funcs.go

+43-1
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,49 @@ func resourceDockerContainerRead(d *schema.ResourceData, meta interface{}) error
682682
}
683683

684684
func resourceDockerContainerUpdate(d *schema.ResourceData, meta interface{}) error {
685-
// TODO call resourceDockerContainerRead here
685+
attrs := []string{
686+
"restart", "max_retry_count", "cpu_shares", "memory", "cpu_set", "memory_swap",
687+
}
688+
for _, attr := range attrs {
689+
if d.HasChange(attr) {
690+
691+
// TODO update ulimits
692+
// Updating ulimits seems not to work well.
693+
// It succeeds to run `DockerClient.ContainerUpdate` with `ulimit` but actually `ulimit` aren't changed.
694+
// https://github.com/terraform-providers/terraform-provider-docker/pull/236#discussion_r373819536
695+
// ulimits := []*units.Ulimit{}
696+
// if v, ok := d.GetOk("ulimit"); ok {
697+
// ulimits = ulimitsToDockerUlimits(v.(*schema.Set))
698+
// }
699+
700+
updateConfig := container.UpdateConfig{
701+
RestartPolicy: container.RestartPolicy{
702+
Name: d.Get("restart").(string),
703+
MaximumRetryCount: d.Get("max_retry_count").(int),
704+
},
705+
Resources: container.Resources{
706+
CPUShares: int64(d.Get("cpu_shares").(int)),
707+
Memory: int64(d.Get("memory").(int)) * 1024 * 1024,
708+
CpusetCpus: d.Get("cpu_set").(string),
709+
// Ulimits: ulimits,
710+
},
711+
}
712+
713+
if ms, ok := d.GetOk("memory_swap"); ok {
714+
a := int64(ms.(int))
715+
if a > 0 {
716+
a = a * 1024 * 1024
717+
}
718+
updateConfig.Resources.MemorySwap = a
719+
}
720+
client := meta.(*ProviderConfig).DockerClient
721+
_, err := client.ContainerUpdate(context.Background(), d.Id(), updateConfig)
722+
if err != nil {
723+
return fmt.Errorf("Unable to update a container: %w", err)
724+
}
725+
break
726+
}
727+
}
686728
return nil
687729
}
688730

docker/resource_docker_container_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ func TestAccDockerContainer_basic(t *testing.T) {
6666
testAccContainerRunning(resourceName, &c),
6767
),
6868
},
69+
{
70+
Config: testAccDockerContainerUpdateConfig,
71+
Check: resource.ComposeTestCheckFunc(
72+
testAccContainerRunning(resourceName, &c),
73+
),
74+
},
6975
{
7076
ResourceName: "docker_container.foo",
7177
ImportState: true,
@@ -1580,6 +1586,24 @@ resource "docker_container" "foo" {
15801586
}
15811587
`
15821588

1589+
const testAccDockerContainerUpdateConfig = `
1590+
resource "docker_image" "foo" {
1591+
name = "nginx:latest"
1592+
}
1593+
1594+
resource "docker_container" "foo" {
1595+
name = "tf-test"
1596+
image = "${docker_image.foo.latest}"
1597+
1598+
restart = "on-failure"
1599+
max_retry_count = 5
1600+
cpu_shares = 32
1601+
cpu_set = "0-1"
1602+
memory = 512
1603+
memory_swap = 2048
1604+
}
1605+
`
1606+
15831607
const testAccDockerContainerWith2BridgeNetworkConfig = `
15841608
resource "docker_network" "tftest" {
15851609
name = "tftest-contnw"

0 commit comments

Comments
 (0)