kreuzwerker / terraform-provider-docker

Terraform Docker provider
Mozilla Public License 2.0
570 stars 187 forks source link

Docker Containers Destroyed if Stopped When Refreshing State #590

Open ssigwart opened 6 months ago

ssigwart commented 6 months ago

Community Note

Terraform (and docker Provider) Version

Terraform v1.4.6
on darwin_amd64
+ provider registry.terraform.io/kreuzwerker/docker v3.0.2

Affected Resource(s)

Terraform Configuration Files

terraform {
    required_providers {
        docker = {
            source = "kreuzwerker/docker"
            version = "3.0.2"
        }
    }
}

data "docker_registry_image" "redis" {
    name = "redis:7-alpine"
}

resource "docker_image" "redis" {
    name = "redis:7-alpine"
    keep_locally = true
    pull_triggers = [data.docker_registry_image.redis.sha256_digest]
}

resource "docker_container" "redis-container-2" {
    image = docker_image.redis.image_id
    name = "redis-2"
    hostname = "redis-2"
    networks_advanced {
        name = "my-network"
    }
    restart = "unless-stopped"
}

Debug Output

Here are relevant snippets

2023-12-16T13:05:37.433-0500 [INFO]  provider.terraform-provider-docker_v3.0.2: 2023/12/16 13:05:37 [DEBUG] Container 4c1f15accff1abf8935dc508a3210c1859ea2930a4766668b490355383764b56 was not created: timestamp=2023-12-16T13:05:37.432-0500
2023-12-16T13:05:37.433-0500 [INFO]  provider.terraform-provider-docker_v3.0.2: 2023/12/16 13:05:37 [INFO] Stopping Container '4c1f15accff1abf8935dc508a3210c1859ea2930a4766668b490355383764b56' with timeout 0s: timestamp=2023-12-16T13:05:37.432-0500
2023-12-16T13:05:37.451-0500 [WARN]  Provider "registry.terraform.io/kreuzwerker/docker" produced an unexpected new value for docker_container.redis-container-2 during refresh.
      - Root resource was present, but now absent
2023-12-16T13:05:37.462-0500 [INFO]  provider.terraform-provider-docker_v3.0.2: 2023/12/16 13:05:37 [INFO] Removing Container '4c1f15accff1abf8935dc508a3210c1859ea2930a4766668b490355383764b56': timestamp=2023-12-16T13:05:37.436-0500
2023-12-16T13:05:37.463-0500 [INFO]  provider.terraform-provider-docker_v3.0.2: 2023/12/16 13:05:37 [INFO] Waiting for Container '4c1f15accff1abf8935dc508a3210c1859ea2930a4766668b490355383764b56' with condition 'not-running': timestamp=2023-12-16T13:05:37.447-0500
2023-12-16T13:05:37.463-0500 [INFO]  provider.terraform-provider-docker_v3.0.2: 2023/12/16 13:05:37 [INFO] Waiting for Container '4c1f15accff1abf8935dc508a3210c1859ea2930a4766668b490355383764b56' errord: 'Error response from daemon: No such container: 4c1f15accff1abf8935dc508a3210c1859ea2930a4766668b490355383764b56': timestamp=2023-12-16T13:05:37.451-0500

Expected Behaviour

If a container is stopped and you run docker plan or docker apply without confirming, the docker container should remain unchanged. If the plan is applied, the existing container should be started if there are no other changes or replaced if there are config changes that require replacing it.

Actual Behaviour

The stopped container is destroyed.

Steps to Reproduce

  1. Run terraform apply
  2. Stop the container in docker.
  3. Run terraform plan or terraform apply. You'll notice in docker that the container is destroyed.

References

Based on the debug logs, these blocks of code look relevant.

The code noticed that the container is not running here: https://github.com/kreuzwerker/terraform-provider-docker/blob/54685a5fb9b375961620d2c555c7f3925327112c/internal/provider/resource_docker_container_funcs.go#L790-L791

That then triggers the container to be deleted here: https://github.com/kreuzwerker/terraform-provider-docker/blob/54685a5fb9b375961620d2c555c7f3925327112c/internal/provider/resource_docker_container_funcs.go#L617-L619

If I add must_run = false to the container, the following line triggers and prevents it from deleting the container. However, it also does not restart the container when I run terraform apply, so I don't think that's the solution to this issue. https://github.com/kreuzwerker/terraform-provider-docker/blob/54685a5fb9b375961620d2c555c7f3925327112c/internal/provider/resource_docker_container_funcs.go#L783-L788

ssigwart commented 6 months ago

I'd be willing to attempt to fix this if a maintainer confirmed that what I have listed as the "Expected Behaviour" above is the desired behavior.

rvsit commented 2 months ago

To add, I think deleting the container in a "plan" stage (which is not supposed to make any changes) creates quite some problems. E.g. I encountered this issue while not noticing that the container had crashed and by deleting the container docker also cleans up the logs, which prevents me from debugging the issue and I should have been warned before that was attempted.