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

  • Using gitlab ci with docker compose leaves me with container generated folders gitlab can't remove
  • Determine if docker image built locally or pulled from repo?
  • Link images on Docker Hub
  • Automate adding new repository tags to Docker Registry Automated Build
  • Why docker containers can run, despite of docker-machine is not running?
  • How to install docker-engine 1.13 on debian Jessie
  • multiple docker compose container - single ip
  • ubuntu automatic stop a daemon doesnt happening
  • Unable to register docker selenium remote node to selenium hub remote host with docker
  • Vagrant Rsync does not sync the folder between host and guest
  • docker is using the v1 registry api when it should use v2
  • AWS Elastic Beanstalk Application and Static Assets Deployment Isolation
  • 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.