From 661b8580b807e558208c6a4fd1e9f482290e4fe0 Mon Sep 17 00:00:00 2001 From: Boris HUISGEN Date: Mon, 8 Oct 2018 20:14:01 +0200 Subject: [PATCH 1/3] Add capability to not start container (create only) Signed-off-by: Boris HUISGEN --- docker/resource_docker_container.go | 6 ++++++ docker/resource_docker_container_funcs.go | 10 ++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/docker/resource_docker_container.go b/docker/resource_docker_container.go index f853ba75..27a4a23c 100644 --- a/docker/resource_docker_container.go +++ b/docker/resource_docker_container.go @@ -18,6 +18,12 @@ func resourceDockerContainer() *schema.Resource { ForceNew: true, }, + "start": &schema.Schema{ + Type: schema.TypeBool, + Default: true, + Optional: true, + }, + // Indicates whether the container must be running. // // An assumption is made that configured containers diff --git a/docker/resource_docker_container_funcs.go b/docker/resource_docker_container_funcs.go index ca4ca9a4..5e55706d 100644 --- a/docker/resource_docker_container_funcs.go +++ b/docker/resource_docker_container_funcs.go @@ -262,10 +262,12 @@ func resourceDockerContainerCreate(d *schema.ResourceData, meta interface{}) err } } - creationTime = time.Now() - options := types.ContainerStartOptions{} - if err := client.ContainerStart(context.Background(), retContainer.ID, options); err != nil { - return fmt.Errorf("Unable to start container: %s", err) + if d.Get("start").(bool) { + creationTime = time.Now() + options := types.ContainerStartOptions{} + if err := client.ContainerStart(context.Background(), retContainer.ID, options); err != nil { + return fmt.Errorf("Unable to start container: %s", err) + } } return resourceDockerContainerRead(d, meta) From a39c7a28252f86c1fbb1444d60093bfd6fadf94f Mon Sep 17 00:00:00 2001 From: Boris HUISGEN Date: Mon, 8 Oct 2018 20:14:30 +0200 Subject: [PATCH 2/3] Add test Signed-off-by: Boris HUISGEN --- docker/resource_docker_container_test.go | 57 ++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/docker/resource_docker_container_test.go b/docker/resource_docker_container_test.go index 3d5a425c..86b5b99b 100644 --- a/docker/resource_docker_container_test.go +++ b/docker/resource_docker_container_test.go @@ -613,6 +613,23 @@ func TestAccDockerContainer_multiple_ports(t *testing.T) { }) } +func TestAccDockerContainer_nostart(t *testing.T) { + var c types.ContainerJSON + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccDockerContainerNoStartConfig, + Check: resource.ComposeTestCheckFunc( + testAccContainerNotRunning("docker_container.foo", &c), + ), + }, + }, + }) +} + func testAccContainerRunning(n string, container *types.ContainerJSON) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] @@ -645,6 +662,33 @@ func testAccContainerRunning(n string, container *types.ContainerJSON) resource. } } +func testAccContainerNotRunning(n string, container *types.ContainerJSON) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No ID is set") + } + + client := testAccProvider.Meta().(*ProviderConfig).DockerClient + containers, err := client.ContainerList(context.Background(), types.ContainerListOptions{}) + if err != nil { + return err + } + + for _, c := range containers { + if c.ID == rs.Primary.ID { + return fmt.Errorf("Container found: %s", rs.Primary.ID) + } + } + + return nil + } +} + func testValueHigherEqualThan(name, key string, value int) resource.TestCheckFunc { return func(s *terraform.State) error { ms := s.RootModule() @@ -885,3 +929,16 @@ resource "docker_container" "foo" { ] } ` +const testAccDockerContainerNoStartConfig = ` +resource "docker_image" "foo" { + name = "nginx:latest" + keep_locally = true +} + +resource "docker_container" "foo" { + name = "tf-test" + image = "nginx:latest" + start = false + must_run = false +} +` From 63ddd271575f14ff8d07b4b991581b85924f0254 Mon Sep 17 00:00:00 2001 From: Boris HUISGEN Date: Mon, 8 Oct 2018 20:14:36 +0200 Subject: [PATCH 3/3] Update doc Signed-off-by: Boris HUISGEN --- website/docs/r/container.html.markdown | 2 ++ 1 file changed, 2 insertions(+) diff --git a/website/docs/r/container.html.markdown b/website/docs/r/container.html.markdown index 72558fa6..15f2e32e 100644 --- a/website/docs/r/container.html.markdown +++ b/website/docs/r/container.html.markdown @@ -65,6 +65,8 @@ data is stored in them. See [the docker documentation][linkdoc] for more details one of "no", "on-failure", "always", "unless-stopped". * `max_retry_count` - (Optional, int) The maximum amount of times to an attempt a restart when `restart` is set to "on-failure" +* `start` - (Optional, bool) If true, then the Docker container will be + started after creation. If false, then the container is only created. * `must_run` - (Optional, bool) If true, then the Docker container will be kept running. If false, then as long as the container exists, Terraform assumes it is successful.