hashicorp / terraform-provider-docker

As part of our introduction to self-service publishing in the Terraform Registry, this copy of the provider has been archived, and ownership has been transferred to active maintainers in the community. Please see the new location on the Terraform Registry: https://registry.terraform.io/providers/kreuzwerker/docker/latest
https://registry.terraform.io/providers/kreuzwerker/docker/latest
Mozilla Public License 2.0
132 stars 92 forks source link

Ports on stopped container force replacement #211

Open Temikus opened 4 years ago

Temikus commented 4 years ago

Looks like this may be a reoccurrence of https://github.com/hashicorp/terraform/issues/19294

λ terraform -v
Terraform v0.12.12
+ provider.docker v2.5.0

Starting with a recent upgrade of the provider (sadly I do not remember which one I've upgraded from) docker provider started forcing recreation of containers that have ports published.

Repro:

1) Fire up this example container:

resource "docker_container" "btsync" {
       image = "resilio/sync"
       name = "btsync"
       capabilities {
         add  = ["NET_ADMIN"]
       }
       ports {
              internal = 8888
              external = 8888
       }
       ports {
              internal = 55555
       }
       log_driver = "json-file"
       log_opts = {
              max-size = "10m"
              max-file = 3
       }
       volumes {
              host_path = "/etc/localtime"
              container_path = "/etc/localtime"
              read_only = true
              }
       restart= "on-failure"
       max_retry_count = 3
       # Do not ensure that the container is running
       must_run="false"
}

2) terraform apply 3) ssh onto the host 4) docker kill btsync 5) terraform plan EXPECTED BEHAVIOUR:

Note that this doesn't happen with another container that's also stopped but doesn't have ports defined, here's the example config:

resource "docker_container" "speedtest_exporter" {
  image = "nlamirault/speedtest_exporter"
  name = "speedtest"
  hostname = "speedtest"

  networks_advanced {
    name = docker_network.homelab.name
    aliases = ["speedtest", "speedtest.docker"]
  }

  volumes {
    host_path = "/etc/localtime"
    container_path = "/etc/localtime"
    read_only = true
  }

  restart= "on-failure"
  max_retry_count = 3
  # Do not ensure that the container needs to be running
  must_run="false"
}
mavogel commented 4 years ago

Yes, we need to revisit this in #138 which will probably be a BC. Sorry but the feature of #103 causes too much trouble. It is very likely a bug in the plugin-sdk: https://github.com/hashicorp/terraform-plugin-sdk/issues/195

Temikus commented 4 years ago

@mavogel Thanks for the quick response!

Just to check, what does ‘BC’ mean in your context?

mavogel commented 4 years ago

Sorry for the abbreviations: BC -> Breaking Change

telefax commented 4 years ago

I'm struggling with this on my running containers, it forces me to re-create the container for each terraform run. It also wants to replace my volumes as well.

Terraform v0.12.19

engie-tawfik commented 4 years ago

I just started learning Terraform yesterday and my instructor uses v 0.11 but I am using 0.12. The first bulk of lessons focus on docker, and with each $ terraform apply (even without any change to my *.tf file) the container is replaced with a new one although the previous container was working fine. In my opinion this kills the concept of idempotency in infrastructure as code.

Versions: Terraform v0.12.24

mavogel commented 4 years ago

You're right @etattw it is currently a bug in v2.7.0 and not intended to replace to container each time although the tf did not change. See #242