I am trying to label my docker image with my app version from within my Dockerfile using the LABEL command.
I can write the version into a file with the following:
RUN /opt/app/foo --version > /var/app-version
/var/app-version would now contain a plaintext file with the application version inside, ready for use in my label.
Now I need a way to place this into a docker variable/arg such that I can do something like the following:
LABEL application_version=$APP_VERSION
Or perhaps there is another way to achieve the same?
You're not going to be able set a label using content generated dynamically from something installed inside the container. You can set a label dynamically from your host environment using a build argument, like this:
FROM docker.io/alpine:latest
ARG application_version=1
LABEL application_version=${application_version}
If I build the container like this:
docker build -t myimage .
I will have:
$ docker inspect image myimage | jq '.[0].Config.Labels'
{
"application_version": "1"
}
Whereas if I build the container like this:
docker build --build-arg application_version=3 -t myimage .
I will have:
$ docker inspect image myimage | jq '.[0].Config.Labels'
{
"application_version": "3"
}
If you use the same build argument value to select the version of the software you install in your container, you can ensure that the installed version and the label match.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With