Merges master

This commit is contained in:
Manuel Vogel 2018-10-27 10:09:03 +02:00
commit c9d9f08d54
No known key found for this signature in database
GPG key ID: 533006C7B61DB1CE
4 changed files with 48 additions and 24 deletions

View file

@ -24,6 +24,12 @@ func resourceDockerContainer() *schema.Resource {
Optional: true, Optional: true,
}, },
"start": &schema.Schema{
Type: schema.TypeBool,
Default: true,
Optional: true,
},
// Indicates whether the container must be running. // Indicates whether the container must be running.
// //
// An assumption is made that configured containers // An assumption is made that configured containers

View file

@ -293,14 +293,16 @@ func resourceDockerContainerCreate(d *schema.ResourceData, meta interface{}) err
} }
} }
ctx := context.Background() if d.Get("start").(bool) {
creationTime = time.Now() creationTime = time.Now()
if err := client.ContainerStart(ctx, retContainer.ID, types.ContainerStartOptions{}); err != nil { options := types.ContainerStartOptions{}
return fmt.Errorf("Unable to start container: %s", err) 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) { 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 { select {
case err := <-errCh: case err := <-errCh:
if err != nil { if err != nil {
@ -342,7 +344,7 @@ func resourceDockerContainerRead(d *schema.ResourceData, meta interface{}) error
} }
jsonObj, _ := json.MarshalIndent(container, "", "\t") 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 || if container.State.Running ||
!container.State.Running && !d.Get("must_run").(bool) { !container.State.Running && !d.Get("must_run").(bool) {

View file

@ -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 { func testAccContainerRunning(n string, container *types.ContainerJSON) resource.TestCheckFunc {
return func(s *terraform.State) error { return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n] rs, ok := s.RootModule().Resources[n]
@ -839,33 +855,19 @@ func testAccContainerNotRunning(n string, container *types.ContainerJSON) resour
if !ok { if !ok {
return fmt.Errorf("Not found: %s", n) return fmt.Errorf("Not found: %s", n)
} }
if rs.Primary.ID == "" { if rs.Primary.ID == "" {
return fmt.Errorf("No ID is set") return fmt.Errorf("No ID is set")
} }
client := testAccProvider.Meta().(*ProviderConfig).DockerClient client := testAccProvider.Meta().(*ProviderConfig).DockerClient
containers, err := client.ContainerList(context.Background(), types.ContainerListOptions{ containers, err := client.ContainerList(context.Background(), types.ContainerListOptions{})
All: true,
})
if err != nil { if err != nil {
return err return err
} }
for _, c := range containers { for _, c := range containers {
if c.ID == rs.Primary.ID { if c.ID == rs.Primary.ID {
inspected, err := client.ContainerInspect(context.Background(), c.ID) return fmt.Errorf("Container found: %s", rs.Primary.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 nil 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
}
`

View file

@ -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". one of "no", "on-failure", "always", "unless-stopped".
* `max_retry_count` - (Optional, int) The maximum amount of times to an attempt * `max_retry_count` - (Optional, int) The maximum amount of times to an attempt
a restart when `restart` is set to "on-failure" 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 * `rm` - (Optional, bool) If true, then the container will be automatically removed after his execution. Terraform won't check this container after creation.
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 * `must_run` - (Optional, bool) If true, then the Docker container will be
kept running. If false, then as long as the container exists, Terraform kept running. If false, then as long as the container exists, Terraform
assumes it is successful. assumes it is successful.