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

Usage in Docker

After configurin…


This content originally appeared on DEV Community and was authored by Smooth Code

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

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)

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

Global Command Setup

Linux Terminal

Linux

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

Windows PowerShell

Windows PowerShell

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

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

Set Date-Timestamps alias:

alias dtsv="date +'%Y_%m_%d__%H_%M_%S'"

Add Date-Timestamps alias to Shell configuration:

nano ~/.bashrc # or nano ~/.zshrc

After editing, source the profile:

source ~/.bashrc # or source ~/.zshrc

Windows PowerShell

Windows PowerShell

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

notepad $PROFILE

Add this function:

function dtsv { Get-Date -Format "yyyy_MM_dd__HH_mm_ss" }

Usage:

dtsv

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


This content originally appeared on DEV Community and was authored by Smooth Code


Print Share Comment Cite Upload Translate Updates
APA

Smooth Code | Sciencx (2025-11-05T13:02:11+00:00) Stop Tagging Docker Images Manually | Automate Docker Tagging full guide. Retrieved from https://www.scien.cx/2025/11/05/stop-tagging-docker-images-manually-automate-docker-tagging-full-guide/

MLA
" » Stop Tagging Docker Images Manually | Automate Docker Tagging full guide." Smooth Code | Sciencx - Wednesday November 5, 2025, https://www.scien.cx/2025/11/05/stop-tagging-docker-images-manually-automate-docker-tagging-full-guide/
HARVARD
Smooth Code | Sciencx Wednesday November 5, 2025 » Stop Tagging Docker Images Manually | Automate Docker Tagging full guide., viewed ,<https://www.scien.cx/2025/11/05/stop-tagging-docker-images-manually-automate-docker-tagging-full-guide/>
VANCOUVER
Smooth Code | Sciencx - » Stop Tagging Docker Images Manually | Automate Docker Tagging full guide. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/11/05/stop-tagging-docker-images-manually-automate-docker-tagging-full-guide/
CHICAGO
" » Stop Tagging Docker Images Manually | Automate Docker Tagging full guide." Smooth Code | Sciencx - Accessed . https://www.scien.cx/2025/11/05/stop-tagging-docker-images-manually-automate-docker-tagging-full-guide/
IEEE
" » Stop Tagging Docker Images Manually | Automate Docker Tagging full guide." Smooth Code | Sciencx [Online]. Available: https://www.scien.cx/2025/11/05/stop-tagging-docker-images-manually-automate-docker-tagging-full-guide/. [Accessed: ]
rf:citation
» Stop Tagging Docker Images Manually | Automate Docker Tagging full guide | Smooth Code | Sciencx | https://www.scien.cx/2025/11/05/stop-tagging-docker-images-manually-automate-docker-tagging-full-guide/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.