kreuzwerker / terraform-provider-docker

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

Support for BUILDKIT Inline Cache #581

Open i2dcarrasco opened 9 months ago

i2dcarrasco commented 9 months ago

Community Note

Description

When building with the docker command and the BUILDKIT_INLINE_CACHE=1 build-arg together with the cache-from option, is caching also the multistage intermediate images. This allows to avoid a full build even if the builder compute doesn't have the image locally (the cache-from option alone will trigger also the full build), avoiding also to create a new image in the remote repository every time.

docker build . -t europe-west1-docker.pkg.dev/.../nginx-base:latest --cache-from europe-west1-docker.pkg.dev/.../nginx-base --build-arg BUILDKIT_INLINE_CACHE=1

In the module I don't know if it's ignoring the cache-from option or the build-arg option, but it's not using the cache and is performing a full build. This only happens when the image does not exists locally (for example after a docker image prune -a). I suppose that the cache-from option is working fine, so I think that maybe the problem is the lack of buildkit support or something similar.

New or Affected Resource(s)

Potential Terraform Configuration

resource "docker_image" "nginx_base" {
  name = format("%s/nginx/nginx-base:latest", module.artifact_registry.artifact_repo_url)
  build {
    context = "./files/src/nginx-base"
    cache_from = [format("%s/nginx/nginx-base", module.artifact_registry.artifact_repo_url)]

    # also tried with build_args
    build_arg = {
      BUILDKIT_INLINE_CACHE : 1
    }

    #tag = [
    #  format("%s/nginx/nginx-base:latest", module.artifact_registry.artifact_repo_url)
    #]
  }

  triggers = {
    dir_sha1 = sha1(join("", [for f in fileset(path.module, "./files/src/nginx-base/*") : filesha1(f)]))
  }
}

References