diff --git a/docker/resource_docker_container.go b/docker/resource_docker_container.go index d5fc0b6e..81e0ab39 100644 --- a/docker/resource_docker_container.go +++ b/docker/resource_docker_container.go @@ -24,6 +24,12 @@ func resourceDockerContainer() *schema.Resource { Optional: 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 6df2a9b0..36698f77 100644 --- a/docker/resource_docker_container_funcs.go +++ b/docker/resource_docker_container_funcs.go @@ -293,14 +293,16 @@ func resourceDockerContainerCreate(d *schema.ResourceData, meta interface{}) err } } - ctx := context.Background() - creationTime = time.Now() - if err := client.ContainerStart(ctx, retContainer.ID, types.ContainerStartOptions{}); 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) + } } if d.Get("attach").(bool) { - statusCh, errCh := client.ContainerWait(ctx, retContainer.ID, container.WaitConditionNotRunning) + statusCh, errCh := client.ContainerWait(context.Background(), retContainer.ID, container.WaitConditionNotRunning) select { case err := <-errCh: if err != nil { @@ -342,7 +344,7 @@ func resourceDockerContainerRead(d *schema.ResourceData, meta interface{}) error } jsonObj, _ := json.MarshalIndent(container, "", "\t") - log.Printf("[INFO] Docker container inspect: %s", jsonObj) + log.Printf("[DEBUG] Docker container inspect: %s", jsonObj) if container.State.Running || !container.State.Running && !d.Get("must_run").(bool) { diff --git a/docker/resource_docker_container_test.go b/docker/resource_docker_container_test.go index bfbad9e2..e7add47d 100644 --- a/docker/resource_docker_container_test.go +++ b/docker/resource_docker_container_test.go @@ -801,6 +801,22 @@ func TestAccDockerContainer_healthcheck(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] @@ -839,33 +855,19 @@ func testAccContainerNotRunning(n string, container *types.ContainerJSON) resour 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{ - All: true, - }) + containers, err := client.ContainerList(context.Background(), types.ContainerListOptions{}) if err != nil { return err } - for _, c := range containers { if c.ID == rs.Primary.ID { - inspected, err := client.ContainerInspect(context.Background(), c.ID) - if err != nil { - return fmt.Errorf("Container could not be inspected: %s", err) - } - *container = inspected - - if container.State.Running { - return fmt.Errorf("Container is running: %s", rs.Primary.ID) - } + return fmt.Errorf("Container found: %s", rs.Primary.ID) } } - return nil } } @@ -1290,3 +1292,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 +} +` diff --git a/website/docs/r/container.html.markdown b/website/docs/r/container.html.markdown index a92a6b91..e9306a92 100644 --- a/website/docs/r/container.html.markdown +++ b/website/docs/r/container.html.markdown @@ -66,8 +66,9 @@ 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" -* `rm` - (Optional, bool) If true, then the container will be automatically removed after his execution. Terraform - won't check this container after creation. +* `rm` - (Optional, bool) If true, then the container will be automatically removed after his execution. Terraform won't check this container after creation. +* `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.