Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing tag as an environment variable inside a Docker container

Tags:

git

docker

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".

like image 665
Erik Avatar asked Jan 24 '15 18:01

Erik


People also ask

How do I pass environment variables to docker containers?

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.

Can docker access environment variables?

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.

Where are environment variables in docker container?

It's not stored (in a file) anywhere. It's passed from Docker to the process running in the container.


2 Answers

This is now possible with docker build arguments.

https://docs.docker.com/engine/reference/commandline/build/

docker build --build-arg TAG=v0.0.1 .

like image 136
Erik Avatar answered Oct 18 '22 18:10

Erik


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.

like image 40
user2915097 Avatar answered Oct 18 '22 19:10

user2915097