Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker Containers Destroyed if Stopped When Refreshing State #590

Open
ssigwart opened this issue Dec 16, 2023 · 2 comments
Open

Docker Containers Destroyed if Stopped When Refreshing State #590

ssigwart opened this issue Dec 16, 2023 · 2 comments

Comments

@ssigwart
Copy link

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

  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:

log.Printf("[DEBUG] Container %s was not created", containerID)
return container, "pending", errContainerFailedToBeCreated

That then triggers the container to be deleted here:

if errors.Is(err, errContainerFailedToBeCreated) {
return resourceDockerContainerDelete(ctx, d, meta)
}

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.

if container.State.Running ||
!container.State.Running && !d.Get("must_run").(bool) {
log.Printf("[DEBUG] Container %s is running: %v", containerID, container.State.Running)
return container, "running", nil
}

@ssigwart
Copy link
Author

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
Copy link

rvsit commented May 3, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants