Accessing tag as an environment variable inside a Docker container
I’d like to be able to access the tag/revision used to build a docker container from within that container. Is there a simple way to surface the tag/revision during the build as an environmental variable?
For example, I would like an API running inside a container to know its current revision. Without docker I would normally use git to write the revision to the a file that the API can access, but this doesn’t seem to be the “Docker way”.
2 Solutions collect form web for “Accessing tag as an environment variable inside a Docker container”
It is not an API, but you can always pass the tag with the
-e of the run command. A short example
$ docker run -it -e mytag=abc123 ubuntu:latest env | grep mytag and so inside your container mytag will contain the tag of the container.
This is now possible with docker build arguments.
docker build --build-arg TAG=v0.0.1 .