How to delete docker images from docker hub using a shell script serverfault
Shell
#!/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
for i in ${REPO_LIST}
#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
- how to remove installation from cent os
- Category
- Shell
- Title
- install zsh debian
- Category
- Shell
- Title
- circleci skip
- Category
- Shell
- Title
- install opencl library
- Category
- Shell
- Title
- github check local branches
- Category
- Shell
- Title
- how use vim in linux
- Category
- Shell
- Title
- firewall-cmd create new zone
- Category
- Shell
- Title
- error: eaccess: permission denied ionic
- Category
- Shell
- Title
- pip install ScraperAPIClient
- Category
- Shell
- Title
- create symbolic link linux
- Category
- Shell
- Title
- javascript execute powershell script
- Category
- Shell
- Title
- updated gitignore not working
- Category
- Shell
- Title
- vuetify install
- Category
- Shell
- Title
- npm run custom script
- Category
- Shell
- Title
- linux df -h levels occupy
- Category
- Shell
- Title
- how to upload files to s3 bucket from mac shell
- Category
- Shell
- Title
- linux how to close terminal full screen
- Category
- Shell
- Title
- register runner gitlab
- Category
- Shell
- Title
- install react react testing library
- Category
- Shell
- Title
- how to install rclone as service on windows
- Category
- Shell
- Title
- locate file in same directory linux
- Category
- Shell
- Title
- git push to branch
- Category
- Shell
- Title
- mysql backup dump docker file
- Category
- Shell
- Title
- git reset soft
- Category
- Shell
- Title
- how to remove node_modules from git
- Category
- Shell
- Title
- change user linuxcommand ch
- Category
- Shell
- Title
- pyaudio python 3.7
- Category
- Shell
- Title
- install openjdk 8 sdk on ubuntu
- Category
- Shell
- Title
- yarn change version
- Category
- Shell
- Title
- set git origin
- Category
- Shell
- Title
- zip current directory mac command line
- Category
- Shell
- Title
- error: src refspec master does not match any. git
- Category
- Shell
- Title
- remove .svn recursively linux
- Category
- Shell
- Title
- npx create-react-app not working
- Category
- Shell
- Title
- npm list global packages
- Category
- Shell
- Title
- conda activate env
- Category
- Shell
- Title
- rtl8821ce driver linux
- Category
- Shell
- Title
- install concrete 5 on ec2 instance
- Category
- Shell
- Title
- linux command to list directory size
- Category
- Shell
- Title
- ignore .DS_store when uploading to s3 using shell
- Category
- Shell
- Title
- timedatectl
- Category
- Shell
- Title
- bash special dollar sign shell varaibles
- Category
- Shell
- Title
- linux convert png favicon
- Category
- Shell
- Title
- free port in linux
- Category
- Shell
- Title
- how to rename a file inside git bash
- Category
- Shell
- Title
- git merge pushed commits
- Category
- Shell
- Title
- how to get application guid powershell
- Category
- Shell
- Title
- setup mysql ubuntu
- Category
- Shell
- Title
- ubuntu ram type
- Category
- Shell
- Title
- edit files from terminal linux
- Category
- Shell
- Title
- scp send file
- Category
- Shell
- Title
- git SSL certificate problem: certificate has expired
- Category
- Shell
- Title
- update ubuntu
- Category
- Shell
- Title
- gulp-gzip
- Category
- Shell
- Title
- wireless network disconnected ubuntu
- Category
- Shell
- Title
- docker build
- Category
- Shell
- Title
- how to validate a mobile number in shell script
- Category
- Shell
- Title
- randomly choose n rows from a file linux
- Category
- Shell
- Title
- install chromedriver linux command line
- Category
- Shell
- Title
- git push
- Category
- Shell
- Title
- how to install lightgbm on mac
- Category
- Shell
- Title
- ubuntu intall OpenBLAS
- Category
- Shell
- Title
- linux check timezone
- Category
- Shell
- Title
- "at" ubuntu script dschedule
- Category
- Shell
- Title
- how to save environment variables ubuntu
- Category
- Shell
- Title
- bash append file
- Category
- Shell
- Title
- install pyenv
- Category
- Shell
- Title
- powershell output text file
- Category
- Shell
- Title
- npm install express-handlebars
- Category
- Shell
- Title
- gumlet/php-image-resize 1.9.2 requires ext-gd *
- Category
- Shell
- Title
- install npm globally
- Category
- Shell
- Title
- run powershell script from wsl bash
- Category
- Shell
- Title
- install ip addr on ubuntu
- Category
- Shell
- Title
- python print sql in shell_plus
- Category
- Shell
- Title
- ubuntu bionic docker container install chrome
- Category
- Shell
- Title
- bash grep and sort
- Category
- Shell
- Title
- wheres i3 config file
- Category
- Shell
- Title
- special bash variables
- Category
- Shell
- Title
- how to pull and overwrite local changes git
- Category
- Shell
- Title
- git ignore still tracking
- Category
- Shell
- Title
- converter mp3 para kali linux
- Category
- Shell
- Title
- linux find file
- Category
- Shell
- Title
- how to install react spring
- Category
- Shell
- Title
- bily-101 github
- Category
- Shell
- Title
- vim compare 2 files
- Category
- Shell
- Title
- fleet management software github
- Category
- Shell
- Title
- navigate to a directory linux
- Category
- Shell
- Title
- install flutter
- Category
- Shell
- Title
- npm ERR! path /usr/local/lib/nodejs/node-v10.15.3-linux-x64/lib/node_modules while installing angular cli
- Category
- Shell
- Title
- search tag git
- Category
- Shell
- Title
- linux command to move a file to another directory
- Category
- Shell
- Title
- gnome-shell turn off
- Category
- Shell
- Title
- how to make a tarball in linux
- Category
- Shell
- Title
- linux inhalt einer textdatei anzeigen
- Category
- Shell
- Title
- git commit ignore eslint
- Category
- Shell
- Title
- apache ubuntu Generate CSR stack overflow
- Category
- Shell
- Title
- how to pip install in folder
- Category
- Shell
- Title
- not reinstalling packages on docker build
- Category
- Shell
- Title
- git clean
- Category
- Shell
- Title
- debian install git server
- Category
- Shell