How to delete docker images older than x days from docker hub using a shell script
Shell
docker image prune -a --filter "until=$(date +'%Y-%m-%dT%H:%M:%S' --date='-15 days')"#!/bin/bash
set -e
# set username and password
UNAME="YOUR_USERNAME"
UPASS="YOUR_PASSWORD"
# get token to be able to talk to Docker Hub
TOKEN=$(curl -s -H "Content-Type: application/json" -X POST -d '{"username": "'${UNAME}'", "password": "'${UPASS}'"}' https://hub.docker.com/v2/users/login/ | jq -r .token)
# get list of namespaces accessible by user (not in use right now)
#NAMESPACES=$(curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/namespaces/ | jq -r '.namespaces|.[]')
#echo $TOKEN
echo
# get list of repos for that user account
echo "List of Repositories in ${UNAME} Docker Hub account"
sleep 5
REPO_LIST=$(curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${UNAME}/?page_size=10000 | jq -r '.results|.[]|.name')
echo $REPO_LIST
echo
# build a list of all images & tags
for i in ${REPO_LIST}
do
# get tags for repo
IMAGE_TAGS=$(curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${UNAME}/${i}/tags/?page_size=10000 | jq -r '.results|.[]|.name')
# build a list of images from tags
for j in ${IMAGE_TAGS}
do
# add each tag to list
FULL_IMAGE_LIST="${FULL_IMAGE_LIST} ${UNAME}/${i}:${j}"
done
done
# output list of all docker images
echo
echo "List of all docker images in ${UNAME} Docker Hub account"
sleep 10
for i in ${FULL_IMAGE_LIST}
do
echo ${i}
done
sleep 10
echo
echo "Identifying and deleting images which are older than 50 days in ${UNAME} docker hub account"
sleep 10
# Note!!! Please un-comment below line if you wanna perform operation on all repositories of your Docker Hub account
#for i in ${REPO_LIST}
for i in randomRepo
#NOTE!!! For deleting Specific repositories images please include only those repositories in for loop like below for loop which has repos mygninx and mykibana
#for i in mynginx mykibana
do
# get tags for repo
echo
echo "Looping Through $i repository in ${UNAME} account"
IMAGE_TAGS=$(curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${UNAME}/${i}/tags/?page_size=10000 | jq -r '.results|.[]|.name')
# build a list of images from tags
for j in ${IMAGE_TAGS}
do
echo
# add last_updated_time
updated_time=$(curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${UNAME}/${i}/tags/${j}/?page_size=10000 | jq -r '.last_updated')
echo $updated_time
datetime=$updated_time
timeago='50 days ago'
dtSec=$(date --date "$datetime" +'%s')
taSec=$(date --date "$timeago" +'%s')
echo "INFO: dtSec=$dtSec, taSec=$taSec"
if [ $dtSec -lt $taSec ]
then
echo "This image ${UNAME}/${i}:${j} is older than 50 days, deleting this image"
## Please uncomment below line to delete docker hub images of docker hub repositories
#curl -s -X DELETE -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${UNAME}/${i}/tags/${j}/
else
echo "This image ${UNAME}/${i}:${j} is within 50 days time range, keep this image"
fi
done
done
echo "Script execution ends"
Also in Shell:
- Title
- register runner gitlab
- Category
- Shell
- Title
- change ratio vmware ubuntu
- Category
- Shell
- Title
- FATAL module ucvideo not found
- Category
- Shell
- Title
- arithmetic operation in bash
- Category
- Shell
- Title
- install bootstrap 4 npm
- Category
- Shell
- Title
- ubuntu delete a symbolic link
- Category
- Shell
- Title
- how to run python in terminal
- Category
- Shell
- Title
- how to unadd gitr file
- Category
- Shell
- Title
- cordova ios emulators
- Category
- Shell
- Title
- list directories recursively powershell
- Category
- Shell
- Title
- linux query port use by pid
- Category
- Shell
- Title
- fcm post example curl
- Category
- Shell
- Title
- docker run command on container
- Category
- Shell
- Title
- pygame not installing in ubuntu 20.04
- Category
- Shell
- Title
- increase open file limit linux mac catalina mariadb
- Category
- Shell
- Title
- linux find file containing text
- Category
- Shell
- Title
- wheres i3 config file
- Category
- Shell
- Title
- set feh wallpaper
- Category
- Shell
- Title
- windows subsystem for linux optional component is not enabled. please enable it and try again
- Category
- Shell
- Title
- htaccess secure
- Category
- Shell
- Title
- centos 7 install iptables
- Category
- Shell
- Title
- edit default port for linux server
- Category
- Shell
- Title
- The repository 'https://download.docker.com/linux/ubuntu focal Release' does not have a Release file
- Category
- Shell
- Title
- Realtek RTL8812BU Network WiFi Adapter kali linux driver
- Category
- Shell
- Title
- ngb-tabset install
- Category
- Shell
- Title
- install sl
- Category
- Shell
- Title
- create github repo with bash
- Category
- Shell
- Title
- powershell upgrade
- Category
- Shell
- Title
- debian install git server
- Category
- Shell
- Title
- ubuntu wsl windows 10
- Category
- Shell
- Title
- linux remove root access
- Category
- Shell
- Title
- show date linux
- Category
- Shell
- Title
- disable monitor on boot linux
- Category
- Shell
- Title
- cordova build release signed
- Category
- Shell
- Title
- stop elasticsearch node
- Category
- Shell
- Title
- git sync branch with master
- Category
- Shell
- Title
- zip current directory mac command line
- Category
- Shell
- Title
- ubuntu ram type
- Category
- Shell
- Title
- how to check upstream git
- Category
- Shell
- Title
- improve ubuntu 16.04 performance
- Category
- Shell
- Title
- why all git pull create merge commit
- Category
- Shell
- Title
- find out a branch a parent branch git
- Category
- Shell
- Title
- check if kubernetes is running
- Category
- Shell
- Title
- fraction simplifier
- Category
- Shell
- Title
- how to unstage changes in git
- Category
- Shell
- Title
- documentation gitlab ci en francais
- Category
- Shell
- Title
- check running process in linux
- Category
- Shell
- Title
- git merge branch
- Category
- Shell
- Title
- bash list not
- Category
- Shell
- Title
- conda install from yaml
- Category
- Shell
- Title
- ubuntu 16.04 bluetooth not detecting devices
- Category
- Shell
- Title
- git find out commit for tag
- Category
- Shell
- Title
- obs
- Category
- Shell
- Title
- git push all branches
- Category
- Shell
- Title
- how to set up a git repository locally
- Category
- Shell
- Title
- netstat install
- Category
- Shell
- Title
- git effacer banch
- Category
- Shell
- Title
- install deno
- Category
- Shell
- Title
- Pyrit download command for linux
- Category
- Shell
- Title
- linux check timezone
- Category
- Shell
- Title
- how to list all versions of pip in ubuntu using grep
- Category
- Shell
- Title
- node ffmpeg
- Category
- Shell
- Title
- git remove tag from remote
- Category
- Shell
- Title
- convert dash to underscore windows
- Category
- Shell
- Title
- gatsby-plugin-typography
- Category
- Shell
- Title
- install ubuntu gui on wsl
- Category
- Shell
- Title
- how to install mysql python
- Category
- Shell
- Title
- how to disconnect local repo from remote master
- Category
- Shell
- Title
- install node js ubuntu
- Category
- Shell
- Title
- git clean remote branches
- Category
- Shell
- Title
- install nginx on ubuntu 18.04
- Category
- Shell
- Title
- install node in nvm
- Category
- Shell
- Title
- batch delete folder
- Category
- Shell
- Title
- how to uninstall kernel jupyter
- Category
- Shell
- Title
- git view stash
- Category
- Shell
- Title
- install pytorch cuda 10
- Category
- Shell
- Title
- import jar file
- Category
- Shell
- Title
- bash ls all subdirectories
- Category
- Shell
- Title
- logrotate force rotation
- Category
- Shell
- Title
- reset gui linux
- Category
- Shell
- Title
- git get access to private repository
- Category
- Shell
- Title
- restart service linux crontab
- Category
- Shell
- Title
- add-apt-repository
- Category
- Shell
- Title
- move a branch back in git
- Category
- Shell
- Title
- close all localhost connections
- Category
- Shell
- Title
- adb shell screenshot
- Category
- Shell
- Title
- angular add modulee
- Category
- Shell
- Title
- find and replace in all files in directory centos
- Category
- Shell
- Title
- install putty on ubuntu
- Category
- Shell
- Title
- bash ls exclude substring
- Category
- Shell
- Title
- git not a valid tag name
- Category
- Shell
- Title
- bash grep and sort
- Category
- Shell
- Title
- how to undo a git stash
- Category
- Shell
- Title
- git command to create a branch
- Category
- Shell
- Title
- apt uninstall
- Category
- Shell
- Title
- manjaro network driver installed but not working
- Category
- Shell
- Title
- gatsby image
- Category
- Shell
- Title
- linux loop over all arguments one by one
- Category
- Shell
- Title
- copy contents of multiple files to one file powershell
- Category
- Shell
- Title
- tree process linux commnad
- Category
- Shell