15

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

Erik
  • 281
  • 3
  • 8

3 Answers3

8

This is now possible with docker build arguments.

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

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

PS: For a multistage build, you need to pass it via all build stages.

swateek
  • 6,735
  • 8
  • 34
  • 48
Erik
  • 281
  • 3
  • 8
  • 2
    Interesting but there is no guarantee that TAG will be same as the image tag. In fact the tag can be changed easily after build using `docker tag`. What is needed is really a run-time environment variable that docker would set as part of the `docker run` command, that's when the tag used for the container is known. – Oliver Jan 15 '19 at 02:46
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.

user2915097
  • 30,758
  • 6
  • 57
  • 59
  • 1
    This is close to what I am looking for, but still doesn't enforce that whatever was passed to -t during the build process is surfaced inside the container when ran. I want my automated build to be able to inject this value into the container at build time. – Erik Jan 25 '15 at 18:50
  • You can also have in your Dockerfile a line `ENV mytag xxx`, then inside your running container you will have an environment variable coming from your build time – user2915097 Jan 26 '15 at 06:51
  • Yup, thought of that as well, but that would require my build to edit my Dockerfile. Not the end of the world, but I'm surprised there isn't a simple way to access information about the container from within the container. – Erik Jan 26 '15 at 15:48
0

seems the directly answer is NO
(the ARG/ENV just workaroud)



And I trying to understand why, by compare it to ARG/ENV.

  • ARG/ENV store infomation in storage layer
  • ARG is build-time const
    //runtime can't access in container, but user can inspect image see it
  • ENV is build-time & runtime const
  • image tag is different
  • has nothing on storage layer
    • can't direct access in container
  • some kind of meta data for image (on host)
    • can be change at build-time or run-time (one image, multi tag, or no tag)
      //not in the same dimension with container
      //but the tag name passed to docker build should special (like ARG/ENV)?



at last, I guess it's by design, they want

  • keep tag only in host context (and mark image at anytime)
  • only for image select (not affect container in other way)


BTW, try to understand env and arg (maybe off-topic)
(maybe not entirely accurate)

build-time build-time runtime runtime image container
set get set get inspect inspect
ENV dockerfile env cmdline env in config in config
ARG dockerfile
cmdline
env NA NA in layer NA
tag cmdline NA cmdline NA in meta in config
yurenchen
  • 1,897
  • 19
  • 17