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".
Using –env, -e When we launch our Docker container, we can pass environment variables as key-value pairs directly into the command line using the parameter –env (or its short form -e). As can be seen, the Docker container correctly interprets the variable VARIABLE1.
Docker Compose allows us to pass environment variables in via command line or to define them in our shell. However, it's best to keep these values inside the actual Compose file and out of the command line.
It's not stored (in a file) anywhere. It's passed from Docker to the process running in the container.
This is now possible with docker build arguments.
https://docs.docker.com/engine/reference/commandline/build/
docker build --build-arg TAG=v0.0.1 .
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
mytag=abc123
and so inside your container mytag will contain the tag of the container.
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