You might have run a docker image or two in your days, and it is great. It just works and it’s great!

At work I recently needed to figure out how to obtain the version of an image tagged latest when the image is pulled by a service. The problem, which you might see, is that if an image does not have the version as its tag it is impossible to know which version it corresponds to. If I pull latest, I pull latest, not the version corresponding to latest if that makes sense.

While looking into this, me and my colleague remembered that all docker images have ✨ meta data ✨ in them, this beautiful thing. You know, things like when it was build, what the entrypoint is, custom labels. This is something we can use. By adding custom data we can create an interface by which a version can be sent in the meta data of the image.

Let us take a closer look.

Making it happen

We need to utilize the docker file field LABEL in which we can specify key-value pair strings. Like this

FROM alpine:3.14

LABEL version="v1.0.0"

Running docker inspect on the built image will result in the (truncated) result

{
	"Config": {
		"Labels": {
			"version": "v1.0.0"
		}
	}
}

Now we want to take it to the next level by supplying the wanted version when we build the docker image, instead of having it hard coded which is obviously bad. To do this we can utilize the --build-arg tag.

func main() {
  fmt.Printf("hello world\n")
}