kreuzwerker / terraform-provider-docker

Terraform Docker provider
Mozilla Public License 2.0
568 stars 186 forks source link

docker_image build image even if file context and dockerfile doesn't change #571

Open IlyesDemineExtVeolia opened 11 months ago

IlyesDemineExtVeolia commented 11 months ago

Community Note

Terraform (and docker Provider) Version

Affected Resource(s)

Terraform Configuration Files

resource "docker_image" "this" {
  name = local.ecr_image_name

  build {
    context    = var.source_path
    dockerfile = var.docker_file_path
    build_args = var.build_args
    platform   = var.platform
  }
}

Debug Output

Panic Output

Expected Behaviour

Don't build and deploy if context and dockerfile doesn't change

Actual Behaviour

Build and Deploy at each terraform apply

Steps to Reproduce

  1. terraform apply

Important Factoids

References

vnghia commented 11 months ago

I found that the build will be retriggered if the content of (file, subfolder, etc) your context folder change, which makes sense because the provider does not know which file in the context folder will be used (e.g COPY or ADD could copy them into the image) while building the Dockerfile.

asmisha commented 10 months ago

In my case the build is triggered even if the content has not changed. I suspect that it's auth_config that causes the rebuild due to changing credentials.

samuelcortinhas commented 8 months ago

I've also come across this bug. Our use case was to build an image from a dockerfile in a folder src/example and push it to an AWS ECR repository. We wanted to only rebuild/push image when there were changes to the code in src/example. However, even with the triggers hash is the same, the docker_image is being rebuilt every time.

resource "docker_registry_image" "registry_image" {
  name = docker_image.image.name
}

resource "docker_image" "image" {
  name = "${aws_ecr_repository.repository.repository_url}:latest"

  build {
    context = "${path.module}/src/example"
  }
  triggers = {
    dir_sha1 = sha1(join("", [for f in fileset(path.module, "src/example/**") : filesha1(f)]))
  }
}

Our solution was to rollback to version 2.25.0 of kreuzwerker/docker. (note the above configuration needs changing for this version).

pspot2 commented 3 weeks ago

I guess quite a few of us are currently sitting on 2.25.0 because of this.

See also https://github.com/kreuzwerker/terraform-provider-docker/issues/555

5imun commented 3 weeks ago

@enc In addition this issue there are also #555 and #607 all the same problem might be good to have them in one place and close duplicates...

This issue described in comment by samuelcortinhas is really annoying with CI deployments because virtual machine that runs terraform starts with clean local docker registry so docker_image resource acts like it was as remotely deleted and terraform tries to recreate it running the docker build again (even though image already exists in remote repo), if we could use docker_registry_image for building and uploading images like before it would solve this resource recreation problem on every terraform apply that happens during builds. Currently this kind of optimised behaviour is not possible to achieve after changes from version >2.25.0

y-chen commented 1 week ago

I opened #607.

@samuelcortinhas I had to update to v3+ becuase v2.5.0 started to throw errors out of nothing. Does it still work for you? I guess I will have to give it a try again because with this bug I am keep rebuilding stuff for nothing.

I am using AWS ECR too.

y-chen commented 1 week ago

Boh, I downgraded to v2.25.0 like others and now works again...

I guess we will never update it. :D