terraform-provider-docker icon indicating copy to clipboard operation
terraform-provider-docker copied to clipboard

Local image not updated despite pull_triggers enabled

Open saitho opened this issue 3 years ago • 4 comments

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 v0.14.10
+ provider registry.terraform.io/kreuzwerker/docker v2.11.0

Affected Resource(s)

  • docker_image

Terraform Configuration Files

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

data "docker_registry_image" "test" {
  name = "saitho/test:latest"
}

resource "docker_image" "test" {
  name          = data.docker_registry_image.test.name
  pull_triggers = [data.docker_registry_image.test.sha256_digest]
  keep_locally = true
}

resource "docker_container" "test" {
  image = docker_image.test.latest
  name  = "test"
  restart = "unless-stopped"

  ports {
    external = 1337
    internal = 80
  }
}

Debug Output

https://gist.github.com/saitho/93ec863cde73d2ad7ea04298f17c7592

Panic Output

None.

Expected Behaviour

I pushed a new version to the "latest" tag of my Docker image. A terraform init should pull the latest image and recreate the containers with that version.

Actual Behaviour

While the container is recreated, the latest image is not pulled. Thus the old image is used for the new container. In fact, running docker pull manually will pull the latest image. After that, terraform apply uses the latest version.

Steps to Reproduce

  1. Push a simple webserver image to Docker registry:
FROM nginx:alpine
RUN echo 'test' > /usr/share/nginx/html/index.html
  1. Replace the image saitho/test in the Terraform file above with your image.
  2. Deploy the Terraform file onto a server with Terraform.
  3. terraform init && terraform apply. Approve application with "yes"
  4. curl http://localhost:1337 => returns "test"
  5. Change the echo in the Dockerfile to print "test2" into the HTML file.
  6. Build the image and push it to Docker registry
  7. terraform apply. Approve application with "yes"
  8. curl http://localhost:1337 => should return "test2", but still returns "test"

Important Factoids

None.

References

None.

saitho avatar Apr 10 '21 14:04 saitho

I think the issue lies with searchLocalImages in https://github.com/kreuzwerker/terraform-provider-docker/blob/master/internal/provider/resource_docker_image_funcs.go#L289. If a local image is found, no pull is issued. So it finds the already loaded "latest" image and will not pull again. Maybe we can skip the searchLocalImages step if the image is being rebuild due to the pull_triggers setting (If we can check that)?

saitho avatar Apr 17 '21 22:04 saitho

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days. If you don't want this issue to be closed, please set the label pinned.

github-actions[bot] avatar Jun 17 '21 10:06 github-actions[bot]

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days. If you don't want this issue to be closed, please set the label pinned.

github-actions[bot] avatar Aug 17 '21 10:08 github-actions[bot]

This is now almost 18 months old... Anything new? This breaks my workflow on Coder - I have a new image generated every 24 hours but would want to cache it after the first pull. With keep_locally to true, it never refreshes; without it it pulls every single time.

Edit: the reply by @stephenliberty in https://github.com/kreuzwerker/terraform-provider-docker/pull/178#issuecomment-971400754 solved it for me.

thehedgefrog avatar Sep 18 '22 03:09 thehedgefrog