# Access git tag/commit from Dockerfile during build without git binary

**URL:** <https://forums.docker.com/t/access-git-tag-commit-from-dockerfile-during-build-without-git-binary/77637>\
**Category:** Docker Hub\
**Tags:** build\
**Created:** [July 7, 2019, 9:30pm UTC](https://forums.docker.com/t/access-git-tag-commit-from-dockerfile-during-build-without-git-binary/77637 "2019-07-07T21:30:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nunojpg](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/nunojpg/32/24260_2.png) [@nunojpg](https://forums.docker.com/u/nunojpg)\
**Post date:** [July 7, 2019, 9:30pm UTC](https://forums.docker.com/t/access-git-tag-commit-from-dockerfile-during-build-without-git-binary/77637/1 "2019-07-07T21:30:58Z")

</div>

I would like my Docker image to be aware of the git tag/commit that originated the build, but without having a git binary to read it. Is there any way to pass that information in a variable into the Dockerfile?

---

<div class="post-metadata">

**Author:** ![meyay](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/meyay/32/34000_2.png) [@meyay](https://forums.docker.com/u/meyay)\
**Post date:** [July 8, 2019, 1:03pm UTC](https://forums.docker.com/t/access-git-tag-commit-from-dockerfile-during-build-without-git-binary/77637/2 "2019-07-08T13:03:18Z")

</div>

I assume that the git binary is only missing in your image, but present on your build system.

For automated builds on Docker Hub, I use a pre\_build hook that exports the reference with `VCS_REF=$(git rev-parse --short HEAD)`. Then I pass the variable as a build arg into the build process. Inside the Dockerfile I assign the ARG to a LABEL (your could assign it to an ENV as well).

---

<div class="post-metadata">

**Author:** ![nunojpg](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/nunojpg/32/24260_2.png) [@nunojpg](https://forums.docker.com/u/nunojpg)\
**Post date:** [July 8, 2019, 2:47pm UTC](https://forums.docker.com/t/access-git-tag-commit-from-dockerfile-during-build-without-git-binary/77637/3 "2019-07-08T14:47:32Z")

</div>

Thanks, that is what I was looking.

I think the basic version only requires hooks/build:

```
#!/bin/bash
docker build --build-arg VCS_REF=$(git rev-parse --short HEAD) -f $DOCKERFILE_PATH -t $IMAGE_NAME .
```

---

<div class="post-metadata">

**Author:** ![meyay](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/meyay/32/34000_2.png) [@meyay](https://forums.docker.com/u/meyay)\
**Post date:** [July 8, 2019, 3:06pm UTC](https://forums.docker.com/t/access-git-tag-commit-from-dockerfile-during-build-without-git-binary/77637/4 "2019-07-08T15:06:31Z")

</div>

Agree, it is sufficient to use it direcly in the build hook.

I just have a pre commit hook, because I prepare a couple of details, like  
– modifiying the tag depending on the source branch (`SOURCE_BRANCH=$(git symbolic-ref -q --short HEAD)`)  
– populate a variable to store the build date (`BUILD_DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ")`)  
– change the image tag in the README.MD which will be used as description on Docker Hub (this could be done for an example docker-compose.yml as well)

My images use the build meta-data as suggested from [http://label-schema.org](http://label-schema.org).

---

<div class="post-metadata">

**Author:** ![meyay](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/meyay/32/34000_2.png) [@meyay](https://forums.docker.com/u/meyay)\
**Post date:** [July 8, 2019, 3:34pm UTC](https://forums.docker.com/t/access-git-tag-commit-from-dockerfile-during-build-without-git-binary/77637/5 "2019-07-08T15:34:53Z")

</div>

Seems like the meta-data from [http://label-schema.org](http://label-schema.org) were superseded in favor of [https://github.com/opencontainers/image-spec](https://github.com/opencontainers/image-spec). You can find the list of suggested labels here: [https://github.com/opencontainers/image-spec/blob/master/annotations.md](https://github.com/opencontainers/image-spec/blob/master/annotations.md)

Since [https://microbadger.com](https://microbadger.com) still depends on the labels of [label-schema.org](http://label-schema.org), for time beeing I will stick with those.
