Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find commit hash from within a running docker image

I currently build my images and tag them with the commit hash. I then pull this image and run it (either via the commit hash or the latest tag). How can I find out the commit hash from a running container ?

All I currently know is the container hostname. So if the container is running I can get the digest:

docker inspect --format='{{.Config.Image}}' hostname

Then from the digest I can search on Dockerhub to find the tag linked to the digest (not sure how to do it) and then the tag has the commit hash linked to it.

Unfortunately, if the container is not still active I get the error:

Error: No such object: hostname

Is there any other way to do this?

like image 526
maxisme Avatar asked Mar 02 '23 05:03

maxisme


1 Answers

One of the option that you can try is to set commit hash in the environment variable, so you will able to get the ENV from the image as well.

docker build --build-arg GIT_COMMIT=$(git rev-parse HEAD) -t my_image:$(git rev-parse HEAD) .

and the Dockerfile

FROM alpine
ARG GIT_COMMIT
ENV GIT_COMMIT=$GIT_COMMIT

Now you can get GIT_COMMIT from env

echo "${GIT_COMMIT}"
like image 110
Adiii Avatar answered Mar 05 '23 15:03

Adiii