terraform-provider-docker
terraform-provider-docker copied to clipboard
Docker Containers Destroyed if Stopped When Refreshing State
Community Note
- Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
- Please do not leave "+1" or "me too" comments, they generate extra noise for issue followers and do not help prioritize the request
- If you are interested in working on this issue or have submitted a pull request, please leave a comment
Terraform (and docker Provider) Version
Terraform v1.4.6
on darwin_amd64
+ provider registry.terraform.io/kreuzwerker/docker v3.0.2
Affected Resource(s)
docker_container
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
- Run
terraform apply - Stop the container in docker.
- Run
terraform planorterraform 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