Save the Docker file with the source code. We use shortcuts to add version information to the generated image. Add:
- git commit and branch
- whether it is "dirty", which means that the changes were made locally on the src code from what is in git
- CI version number (public)
- the person who created the image (and not the one who last checked in git)
We also mark the image with a commit number.
Here is our code for one of our services. We use Buildkite for our CI and Quay.io for our image registry.
build-image.sh
echo '===> Building docker image...' GIT_BRANCH=$(git name-rev --name-only HEAD | sed "s/~.*//") GIT_COMMIT=$(git rev-parse HEAD) GIT_COMMIT_SHORT=$(echo $GIT_COMMIT | head -c 8) GIT_DIRTY='false' BUILD_CREATOR=$(git config user.email) BUILD_NUMBER="${BUILDKITE_BUILD_NUMBER-0}"
Dockerfile
FROM ... ARG GIT_COMMIT ARG GIT_BRANCH=master ARG GIT_DIRTY=undefined ARG BUILD_CREATOR ARG BUILD_NUMBER LABEL branch=$GIT_BRANCH \ commit=$GIT_COMMIT \ dirty=$GIT_DIRTY \ build-creator=$BUILD_CREATOR \ build-number=$BUILD_NUMBER ... etc
Then you can create scripts that check the version of your image. For instance:
docker inspect --format "{{.ContainerConfig.Labels.commit}}" imageid
source share