Can you dynamically set a docker image LABEL to a git commit value when building an image with packer?

27 views Asked by At

I am looking at transitioning docker image builds from using a bash wrapper script + Dockerfiles to using packer. The script does a git clone, then sets a variable in the script to a git commit (e.g. gitCommit=$(git log -1 --pretty="format:%h %s")). The variable is then passed to the docker build command as a build arg which is applied to a LABEL in the Dockerfile.

I would like to continue to be able to dynamically set a LABEL (and likely other things) in this fashion, but I do not see anything in the packer documentation that enables this.

Is this functionality possible or is this an accepted downside of packer?

Thanks!

I've read the packer documentation on variables, locals, source.changes (where the LABEL value would get set), and provisioners. I also have had successful builds of the images without setting the LABEL, so the build is working otherwise.

1

There are 1 answers

0
BMitch On

I'm not aware of whether Packer can do this, but solving your underlying issue, it is possible to modify the image after it has been created. Two tools I know of for this are crane (from Google) and regctl (from myself).

crane mutate --label "gitCommit=$(git log -1 --pretty="format:%h %s")" $image

regctl image mod --replace --label "gitCommit=$(git log -1 --pretty="format:%h %s")" $image

Note that this does change the image digest, so you'll want to avoid anything that depends on that (e.g. image signing) until after the image is modified.