diff --git a/resource_docker_container.go b/resource_docker_container.go index 4fe63650..7d2fa34c 100644 --- a/resource_docker_container.go +++ b/resource_docker_container.go @@ -6,6 +6,7 @@ import ( "github.com/hashicorp/terraform/helper/hashcode" "github.com/hashicorp/terraform/helper/schema" + "regexp" ) func resourceDockerContainer() *schema.Resource { @@ -92,6 +93,27 @@ func resourceDockerContainer() *schema.Resource { ForceNew: true, }, + "restart": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + Default: "no", + ValidateFunc: func(v interface{}, k string) (ws []string, es []error) { + value := v.(string) + if !regexp.MustCompile(`^(no|on-failure|always)$`).MatchString(value) { + es = append(es, fmt.Errorf( + "%q must be one of \"no\", \"on-failure\", or \"always\"", k)) + } + return + }, + }, + + "max_retry_count": &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + ForceNew: true, + }, + "volumes": &schema.Schema{ Type: schema.TypeSet, Optional: true, diff --git a/resource_docker_container_funcs.go b/resource_docker_container_funcs.go index 24df6949..800f0f8a 100644 --- a/resource_docker_container_funcs.go +++ b/resource_docker_container_funcs.go @@ -95,6 +95,10 @@ func resourceDockerContainerCreate(d *schema.ResourceData, meta interface{}) err hostConfig := &dc.HostConfig{ Privileged: d.Get("privileged").(bool), PublishAllPorts: d.Get("publish_all_ports").(bool), + RestartPolicy: dc.RestartPolicy{ + Name: d.Get("restart").(string), + MaximumRetryCount: d.Get("max_retry_count").(int), + }, } if len(portBindings) != 0 { diff --git a/resource_docker_container_test.go b/resource_docker_container_test.go index e888c67d..0d0fe734 100644 --- a/resource_docker_container_test.go +++ b/resource_docker_container_test.go @@ -25,7 +25,7 @@ func TestAccDockerContainer_basic(t *testing.T) { }) } -func TestAccDockerContainer_entrypoint(t *testing.T) { +func TestAccDockerContainer_customized(t *testing.T) { var c dc.Container testCheck := func(*terraform.State) error { @@ -35,6 +35,15 @@ func TestAccDockerContainer_entrypoint(t *testing.T) { c.Config.Entrypoint[2] != "ping localhost") { return fmt.Errorf("Container wrong entrypoint: %s", c.Config.Entrypoint) } + + if c.HostConfig.RestartPolicy.Name == "on-failure" { + if c.HostConfig.RestartPolicy.MaximumRetryCount != 5 { + return fmt.Errorf("Container has wrong restart policy max retry count: %d", c.HostConfig.RestartPolicy.MaximumRetryCount) + } + } else { + return fmt.Errorf("Container has wrong restart policy: %s", c.HostConfig.RestartPolicy.Name) + } + return nil } @@ -43,7 +52,7 @@ func TestAccDockerContainer_entrypoint(t *testing.T) { Providers: testAccProviders, Steps: []resource.TestStep{ resource.TestStep{ - Config: testAccDockerContainerEntrypointConfig, + Config: testAccDockerContainerCustomizedConfig, Check: resource.ComposeTestCheckFunc( testAccContainerRunning("docker_container.foo", &c), testCheck, @@ -95,7 +104,7 @@ resource "docker_container" "foo" { image = "${docker_image.foo.latest}" } ` -const testAccDockerContainerEntrypointConfig = ` +const testAccDockerContainerCustomizedConfig = ` resource "docker_image" "foo" { name = "nginx:latest" } @@ -104,5 +113,7 @@ resource "docker_container" "foo" { name = "tf-test" image = "${docker_image.foo.latest}" entrypoint = ["/bin/bash", "-c", "ping localhost"] + restart = "on-failure" + max_retry_count = 5 } `