DEV Community

Cover image for Stop Tagging Docker Images Manually | Automate Docker Tagging full guide
Smooth Code
Smooth Code

Posted on

Stop Tagging Docker Images Manually | Automate Docker Tagging full guide

This Article provides a streamlined approach to automate Docker image tagging using versioning strategies.

It supports two main versioning approaches:

  • Semantic Versioning

  • Timestamp-based Versioning

Semantic and Timestamp-based Versioning

Usage in Docker

After configuring Semantic/DateTimeStamp versioning, you can use it in docker like the following

docker build -t "imagename:$(pyv)" . #build docker image with semantic versioning
docker push "username/imagename:$(pyv)" #push docker image created with semantic versioning
docker build -t "imagename:$(dtsv)" . #build docker image with TimeStamp based versioning
Enter fullscreen mode Exit fullscreen mode

Docker Image Reference Structure

Docker Image Reference Structure

🔹 REGISTRY_HOST:PORT (Optional): Specifies the hostname (and optional port) of the Docker registry.
🔹 NAMESPACE (Optional): Represents the user or organization account within the registry.
🔹 REPOSITORY (Required): The actual name of the image.
🔹 TAG (Optional but Important): Identifies a specific version or variant of the image.
myapp:1.0.0 (semantic version)
myapp:2025_10_14_1200 (timestamp-based tag)
myapp:latest (default if none specified)

Semantic Versioning

Semantic versioning is a widely adopted version scheme that encodes a version by a three-part version number (Major. Minor. Patch), an optional pre-release tag, and an optional build meta tag. In this scheme, risk and functionality are the measures of significance.

Semantic Versioning

The core script pyv.py manages semantic versioning stored in a docker_version.json file. It allows incrementing major, minor, and patch versions, and displays the current version.

import json
import sys
import os

default_version={"major":1,"minor":0,"patch":0}

VERSION_FILE="docker_version.json"

def get_version():
    if not os.path.exists(VERSION_FILE):
        return default_version
    with open(VERSION_FILE,"r") as f:
        return json.load(f)
def save_version(version):
    with open(VERSION_FILE,"w") as f:
        json.dump(version,f,indent=2)

def format_version(version):
    major=version["major"]
    minor=version["minor"]
    patch=version["patch"]
    if patch==0:
        if minor==0:
            return f"{major}"
        else:
            return f"{major}.{minor}"
    else:
        return f"{major}.{minor}.{patch}"

def show_version():
    v=get_version()
    return format_version(v)

def update_version(version):
    save_version(version)
    return format_version(version)

def increment_major():
    v=get_version()
    v["major"]+=1
    v["minor"]=0
    v["patch"]=0
    return update_version(v)

def increment_minor():
    v=get_version()
    v["minor"]+=1
    v["patch"]=0
    return update_version(v)

def increment_patch():
    v=get_version()
    v["patch"]+=1
    return update_version(v)

if __name__ == '__main__':

    if len(sys.argv)==1:
        print(show_version())
        sys.exit(0)
    if len(sys.argv)>2:
        print("Usage: pyv <major|minor|patch|show>")
        sys.exit(1)

    command=sys.argv[1]

    if command=="major":
        print(increment_major())
    elif command=="minor":
        print(increment_minor())
    elif command=="patch":
        print(increment_patch())
    elif command=="show":
        print(show_version())
    else:
        print("Invalid command. Use major|minor|patch|show as argument.")
        sys.exit(1)
Enter fullscreen mode Exit fullscreen mode

How it works

  • If the version file does not exist, it initializes to version 1.0.0. Supports commands: show: Displays current version, major: Increments major version, minor: Increments minor version, patch: Increments patch version

Usage

python pyv.py show
python pyv.py major
python pyv.py minor
python pyv.py patch
Enter fullscreen mode Exit fullscreen mode

Global Command Setup

Linux Terminal

Linux

  • Create a pyv script (without.py extension)
#!/usr/bin/env python3
# Add the Python script code here
Enter fullscreen mode Exit fullscreen mode
  • Make it executable and copy it to /usr/local/bin/
chmod +x pyv
sudo cp pyv /usr/local/bin/
Enter fullscreen mode Exit fullscreen mode

Windows PowerShell

Windows PowerShell

  • Create a batch file (pyv.bat)
@echo off
python "%~dp0pyv.py" %*
Enter fullscreen mode Exit fullscreen mode
  • Set up PowerShell Profile
Test-Path $PROFILE
New-Item -ItemType File -Path $PROFILE -Force
notepad $PROFILE
Enter fullscreen mode Exit fullscreen mode
  • Add alias to profile
Set-Alias pyv "C:\path\to\pyv.bat" #copy the absolute path of your pyv.bat file
Enter fullscreen mode Exit fullscreen mode
  • Configure execution policy (if you are having restrected execution policy)
Get-ExecutionPolicy
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Enter fullscreen mode Exit fullscreen mode

Date Timestamps

This format automatically tags using timestamps in the pattern Year_Month_Day__Hour_Minute_Second to ensure unique and chronologically ordered names.

Date Timestamps Versioning

Linux

echo $SHELL #To know your shell
Enter fullscreen mode Exit fullscreen mode

Set Date-Timestamps alias:

alias dtsv="date +'%Y_%m_%d__%H_%M_%S'"
Enter fullscreen mode Exit fullscreen mode

Add Date-Timestamps alias to Shell configuration:

nano ~/.bashrc # or nano ~/.zshrc
Enter fullscreen mode Exit fullscreen mode

After editing, source the profile:

source ~/.bashrc # or source ~/.zshrc
Enter fullscreen mode Exit fullscreen mode

Windows PowerShell

Windows PowerShell

Add the following function to your PowerShell profile to generate timestamp strings:

notepad $PROFILE
Enter fullscreen mode Exit fullscreen mode

Add this function:

function dtsv { Get-Date -Format "yyyy_MM_dd__HH_mm_ss" }
Enter fullscreen mode Exit fullscreen mode

Usage:

dtsv
Enter fullscreen mode Exit fullscreen mode

This setup enables efficient Docker image tagging through semantic or timestamp-based versioning, with cross-platform support and easy integration into your build process.

Top comments (0)