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

  • can't pull docker images from nexus proxy
  • What's causing authentication error when pushing Docker image to Google Container Registry?
  • error: unable to unlink old 'gulpfile.js' (Permission denied) while using Docker Compose for development environment
  • dockerize c#.NET desktop application
  • docker nginx stream balancer 404
  • TravisCI build succeeds even when tests fail
  • 12factor config approach with Docker
  • How to pass parameters from docker to angular js?
  • Jenkins - Docker integration - Use Jenkins to build Docker images and push to the registry
  • Docker: Error, Container command 'docker-entrypoint.sh' not found or does not exist
  • Docker, dockercloud and Cron
  • golang:sshfs -o reconnect fails when used inside docker container with “reading directory .: Input/output error”
  • 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.