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

  • Docker capture packets
  • How can I access internet in docker build?
  • Docker exec in bash script
  • Error while setting up docker dev env
  • docker pull centos timeout on windows 8.1
  • How to specify different .dockerignore files for different builds in the same project?
  • REPLICAS remains 0 after running `docker stack deploy` command in manager machine
  • Error running gitlab-ci.yml script under Alpine Docker based Image
  • How to connect Cassandra and Node application in docker?
  • Dockerize Spring Boot Java app with Kafka, Zookeeper, and MongoDB [closed]
  • Linking containers between task definitions in AWS ECS?
  • Update (pull) all docker images at once
  • 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
    mytag=abc123
    and so inside your container mytag will contain the tag of 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 .

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.