How to Tag a Docker Image on Codefresh

Nwokocha Wisdom Maduabuchi
5 min readAug 26, 2021

--

https://www.slideshare.net/Codefresh/docker-basedpipelines-with-codefresh

Docker tags are unique alias to your Image ID that make it easy to manage version control and refer to your image and releases; the tags look like these my_docker_image:3 where my_docker_image is the docker image name, and 3 is the tag; it's always after the colon(:).

If you don’t specify a specific tag while tagging your docker image, Docker automatically tags it as “latest.”

Tagging a docker image is easy with the following command

docker build -t repository/image:tag .

The above command tells Docker to build the image from the Dockerfile and tag it with the tag you specified. Remember I said that the tag is the part that comes after the semicolon(:), which is (tag) in the command above.

Tag Image in Codefresh

It is much easier to tag an image on the Codefresh platform, but before we proceed, you will need to know the following steps to tag an Image on the Codefresh platform successfully.

  • Have a DockerHub account
  • know about Codefresh Variables
  • Know how to build and push an Image

Firstly, you will need a Dockerhub account to store your docker image on your repository; for more info, check this link and this link2.

Secondly, you will have a good understanding of a Codefresh variable; for more info, check this link.

Thirdly, you will have a good understanding of how to build and push a docker image to any registry of your choice; for more info, check this link.

This article will show three different ways to tag your docker image on the Codefresh platform.

  1. Tag Image with gitHash
  2. Tag Image with Build ID
  3. Tag Image with App Version ID

If you read this link: https://codefresh.io/docs/docs/codefresh-yaml/variables/, it was stated that Codefresh provides a set of predefined variables automatically in each build; you will use some of them to tag your docker image.

Tag Image with gitHash:

To tag an image with githash is very easy using the predefined variable on Codefresh called “CF_REVISION” you will change the tag in your build

“Push:
type: push
stage: push
arguments:
candidate: ‘${{build_my_app}}’
tag: ‘${{CF_REVISION}}’
image_name: wise4rmgod/my-node-js-app
registry: dockerhub”

version: "1.0"
# Stages can help you organize your steps in stages
stages:
- "clone"
- "build"
- "push"
steps:
clone:
title: "Cloning repository"
type: "git-clone"
repo: "wise4rmgod/NodeDocker_Example"
# CF_BRANCH value is auto set when pipeline is triggered
# Learn more at codefresh.io/docs/docs/codefresh-yaml/variables/
revision: "${{CF_BRANCH}}"
git: "github"
stage: clone
build_my_app:
title: Building Node.Js Docker Image
type: build
stage: build
# image_name: my-node-js-app
working_directory: './NodeDocker_Example'
arguments:
image_name: '${{CF_REPO_OWNER}}/${{CF_REPO_NAME}}'
tag: master
dockerfile: Dockerfile

Push:
type: push
stage: push
arguments:
candidate: '${{build_my_app}}'
# tag: 'v1.0.0'
tag: '${{CF_REVISION}}'
image_name: wise4rmgod/my-node-js-app
registry: dockerhub

Pros:

  • It is easy to identify the images
  • The image will not be overwritten since the Git commit hash are unique

Cons:

  • It will quickly lead to a cluttered registry.
  • Not easy to track the latest Image

Tag Image with Build ID:

To tag an image with Build_ID is very easy using the predefined variable on Codefresh called “CF_BUILD_ID” you will change the tag in your build

“Push:
type: push
stage: push
arguments:
candidate: ‘${{build_my_app}}’
tag: ‘${{CF_BUILD_ID}}’
image_name: wise4rmgod/my-node-js-app
registry: dockerhub “

Here is the most basic pipeline that tags a docker image with the App version and builds the image:

codefresh.yml

version: "1.0"
# Stages can help you organize your steps in stages
stages:
- "clone"
- "build"
- "push"
steps:
clone:
title: "Cloning repository"
type: "git-clone"
repo: "wise4rmgod/NodeDocker_Example"
# CF_BRANCH value is auto set when pipeline is triggered
# Learn more at codefresh.io/docs/docs/codefresh-yaml/variables/
revision: "${{CF_BRANCH}}"
git: "github"
stage: clone
build_my_app:
title: Building Node.Js Docker Image
type: build
stage: build
# image_name: my-node-js-app
working_directory: './NodeDocker_Example'
arguments:
image_name: '${{CF_REPO_OWNER}}/${{CF_REPO_NAME}}'
tag: master
dockerfile: Dockerfile

Push:
type: push
stage: push
arguments:
candidate: '${{build_my_app}}'
# tag: 'v1.0.0'
tag: '${{CF_BUILD_ID}}'
image_name: wise4rmgod/my-node-js-app
registry: dockerhub

Pros: It doesn't overwrite the recent image

Cons: It’s not easy to track the latest image

Tag Image with App Version ID:

You will get the app version from the package.json file in this section to tag an image with App Version ID. You will use the “kostis-codefresh/node-version” step in the Codefresh step marketplace. The step does two main operations, which are:
1) read_app_version: this operation reads the app version from the package.json file
2) print_app_version: this operation prints the version ID

“Push:
type: push
stage: push
arguments:
candidate: ‘${{build_my_app}}’
tag: verion
image_name: wise4rmgod/my-node-js-app
registry: dockerhub “

Here is the most basic pipeline that tags a docker image with the App version and builds the image:

codefresh.yml

version: "1.0"
# Stages can help you organize your steps in stages
stages:
- "clone"
- "build"
- "push"
steps:
clone:
title: "Cloning repository"
type: "git-clone"
repo: "wise4rmgod/NodeDocker_Example"
# CF_BRANCH value is auto set when pipeline is triggered
# Learn more at codefresh.io/docs/docs/codefresh-yaml/variables/
revision: "${{CF_BRANCH}}"
git: "github"
stage: clone
build_my_app:
title: Building Node.Js Docker Image
type: build
stage: build
# image_name: my-node-js-app
working_directory: './NodeDocker_Example'
arguments:
image_name: '${{CF_REPO_OWNER}}/${{CF_REPO_NAME}}'
tag: master
dockerfile: Dockerfile

read_app_version:
title: Reading app version
type: kostis-codefresh/node-version
arguments:
PACKAGE_JSON_FOLDER: ./NodeDocker_Example
print_app_version:
title: Printing app version
image: alpine
commands:
- echo $APP_VERSION

Push:
type: push
stage: push
arguments:
candidate: '${{build_my_app}}'
# tag: 'v1.0.0'
tag: print_app_version
image_name: wise4rmgod/my-node-js-app
registry: dockerhub

Pros:

  • It's easy to track the latest image

Cons:

  • It overwrites the recent image

After choosing how to tag your Image on Codefresh, the next step is to RUN your Pipeline. The screen below will be the result after running your pipeline.

Check your docker hub account to see if you successfully pushed the Image.

Conclusion

Tagging Docker images properly is very important. After all, it will help you reference your image for easy access and keep it because it was overwritten due to having the same Image Tag.

Remember, if you don’t Tag your image it will be name “latest” even an old image will also be “latest”

Also, Tagging doesn't have to be manual and stressful; you can automate the process using Codefresh by adding a few Steps, and you are good to go. This is because there are so many ways to easily tag your Docker image in Codefresh, making it the best platform for this.

--

--

Nwokocha Wisdom Maduabuchi
Nwokocha Wisdom Maduabuchi

Written by Nwokocha Wisdom Maduabuchi

A software engineer with considerable experience in mobile development, native Android, and IOS development(Xcode), flutter dev, technical writing and community

No responses yet