Code deduplication for baking files (HCL format)
Hello! Is there any way to dedupe configuration in HCL files, such as for_each instructions? The HCL file size starts to explode on a scale (1000+ lines - easy), for example, if we have multiple Dockerfiles formed nearly identical. Each Dockerfile has the final build stage for the container image. Each Dockerfile has a security scanning stage with a report we want to export. Also, we need every container image to be pushed in the container registry and saved locally. In this case, we have an enormous amount of code duplication. Typically it is solved by declaring the list of items and iterating over with any loop operator. We can provide an example if needed.
@johnbizokk
We can provide an example if needed.
Yes that would help. Thanks.
Basically support the for_each argument as in terraform so a resource can be repeated https://www.terraform.io/language/meta-arguments/for_each
And something like setfunction() for building each combination from multiple lists
Unfortunately, we were unable to find a single example of using for_each in a bake HCL file. The lack of documentation and unclear version of the HCL Native Syntax Specification supported by Docker makes it a frustrating experience.
Despite trying recommended examples from the specification, most did not work. As a result, we have decided to use Packer and Docker manifest to generate multi-arch images. The recurring issue with poor documentation and unsupported HCL syntax is a concern and we hope that Docker invests in improving their documentation in the future.
I believe those 2 features could maybe alleviate some of the burden here:
https://github.com/docker/buildx/pull/1690 https://github.com/moby/buildkit/issues/1555
Yes, it indeed works. Super cool! I optimized the baking file (4400 -> 2300 lines) in half on the first try using the matrix targets. Also, documentation on this feature is very informative (https://docs.docker.com/build/bake/reference/#targetmatrix). The only catch - it only works for targets, but now for groups.
@johnbizokk if you have a good use case for using it for groups, then do feel free to open a feature request :tada:
It was originally supported in the first draft of my PR that added it, but eventually removed it since I couldn't think of a reasonable use case for it - if you've got one, we could definitely add it back in :smile: