All Articles

Tagging docker images differently based on git branch

At work we use git flow to organize our git repositories. master is the production branch, develop is the staging branch, and longer features get their own feature/* branch. On these branches, everything is built into docker images that are uploaded to a registry to later be deployed. git flow also gives us some basic version tagging that we want to use to track our releases.

I wanted to tag these docker images so that we could easier refer to them in our deployment. Where there would be a latest tag that would always be the latest version, and maybe a tag for every version.

I came up with a python script that wraps around docker and git to automatically generate these tags for me, with some basic customization. It’s pretty rough, but you can find it here:


docker-branch-tagging init generates a default .docker-branch-tagging file that looks something like the following.

    "develop": ["latest", "develop-{CIRCLE_BUILD_NUM}", "{git_branch}"],
    "feature/(.+)": ["{git_branch}"],
    "master": [

The keys are regex, and the values are python format strings. The values get passed the current environment variables as well as two special case variables git_branch and git_latest_version_tag. The script will simply look for any keys matching the current git branch and perform the templating on the values to generate the different tags.

Doing a docker-branch-tagging build on the master branch would then result in something like the following.

docker build -t -t -t .

docker-branch-tagging push would then perform the docker push. Ungefär like so:

docker push
docker push
docker push

On CircleCI, which is where we do our continous integration, the step for building and pushing the docker containers generally looks something like this now.

        - image: circleci/python3
        - run: sudo pip install awscli
        - run: sudo pip install "git+"
        - checkout
        - attach_workspace:
              at: .
        - setup_remote_docker
        - run: 'docker login -u AWS -p $(aws ecr get-authorization-token --output text --query authorizationData[].authorizationToken | base64 --decode | cut -d: -f2) $DOCKER_REPOSITORY'
        - run: docker-branch-tagging build $DOCKER_REPOSITORY
        - run: docker-branch-tagging push $DOCKER_REPOSITORY

The attach_workspace is used to copy over whatever dependencies that may have been installed via e.g. npm or binaries/webpacks that may have been built in a previous workflow step. DOCKER_REPOSITORY is the environment variable I set in the project configuration to the AWS ECR uri.