Tagging Dockerfiles and Docker Releases with Git Hashes

Why use Git Commit Tagging for Docker Image Releases?

In an opinionated manner it makes it very easy to reason about what is actually deployed.

It is nice to be able to go to Github or Gitlab and look for a specific commit instead of look for a specific tag.

Utilizing this Makefile releases can easily be automated through a CI/CD tool or done manually (for outages) and still follow a team norm.

Sample Dockerfile

Adding the following build arg to your Dockerfile will add a label for the git commit.


      FROM marquezproject/marquez:0.10.4

      ARG GIT_COMMIT=unspecified
      LABEL git_commit="$GIT_COMMIT"

      COPY marquez-config.yml /usr/src/app/config.yml
      COPY Docker/wait-for-it.sh /usr/src/app/wait-for-it.sh

      EXPOSE 5000 5001

      

The only required lines from this Dockerfile are:


  
        ARG GIT_COMMIT=unspecified
        LABEL git_commit="$GIT_COMMIT"
  
      

Sample Makefile

Please note that this Makefile implies usage with Amazon ECR.


        AWS_REPO	= {ACCOUNT_NUMBER}.dkr.ecr.us-east-1.amazonaws.com/marquez
        REGION		= us-east-1
        LOGIN		= aws ecr get-login --no-include-email --region $(REGION)
        COMMIT		= $(shell git rev-parse HEAD)
        
        build:
          docker build -t marquez:$(COMMIT) . --build-arg GIT_COMMIT=$(COMMIT)
        
        ecr_login:
          $(shell $(LOGIN))
        
        push: ecr_login
          docker push $(AWS_REPO):$(COMMIT)
        
        tag:
          docker tag marquez:$(COMMIT) $(AWS_REPO):$(COMMIT)
        
        release: build \
          ecr_login \
          tag \
          push
        
        help:
          @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
        
        
        .PHONY: build ecr_login push tag release help

      

Please note the only required lines to work with the Dockerfile is (but change `marquez` to your docker name):


        COMMIT		= $(shell git rev-parse HEAD)
        
        build:
          docker build -t marquez:$(COMMIT) . --build-arg GIT_COMMIT=$(COMMIT)
        
        tag:
          docker tag marquez:$(COMMIT) $(AWS_REPO):$(COMMIT)

      

To utilize the Makefile commands one would run Make build && Make tag and then publish to the desired Docker registry.