NOTE: All below example codes are executed with root privilege on ubuntu 20.04 LTS
I use following command to delete emptry repository
docker exec -it registry sh -c '
for t in $(find /var/lib/registry/docker/registry/v2/repositories -name tags)
do
TAGS=$(ls $t | wc -l)
REPO=${t%%\/_manifests\/tags}
LINKS=$(find $REPO/_manifests/revisions -name link | wc -l)
if [ "$TAGS" -eq 0 -a "$LINKS" -eq 0 ]; then
echo "REMOVE empty repo: $REPO"
rm -rf $REPO
fi
done'
docker restart registry
the registry is the registry that runs on the machine.
you can check the name by docker ps
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d3c40930b56b konradkleine/docker-registry-frontend:v2 "/bin/sh -c $START_S…" About an hour ago Up 42 minutes 80/tcp, 0.0.0.0:30443->443/tcp, :::30443->443/tcp reg-ui
787dd0c13058 registry:2.7.1 "/entrypoint.sh /etc…" 5 days ago Up 34 minutes 0.0.0.0:443->443/tcp, :::443->443/tcp, 5000/tcp registry
#
make sure that both the number of tags and links must be zero.
and the docker process must be restarted, if any directories are removed.
If the command works find, you can create a script
cat<<EOM | tee /usr/local/bin/docker-registry-remove-empty-repo.sh
#!/bin/bash
RM_LOG=\$(docker exec -it registry sh -c '
for t in \$(find /var/lib/registry/docker/registry/v2/repositories -name tags)
do
TAGS=\$(ls \$t | wc -l)
REPO=\${t%%\/_manifests\/tags}
LINKS=\$(find \$REPO/_manifests/revisions -name link | wc -l)
if [ "\$TAGS" -eq 0 -a "\$LINKS" -eq 0 ]; then
echo "REMOVE empty repo: \$REPO"
rm -rf \$REPO
fi
done')
if [ -n "\$RM_LOG" ]; then
echo "\$RM_LOG"
docker restart registry
fi
EOM
chmod +x /usr/local/bin/docker-registry-remove-empty-repo.sh
even you can run it every minutes by registring the script to the cron
sudo sed -i '/docker-registry-remove-empty-repo.sh/d' /var/spool/cron/crontabs/$USER
cat <<EOM | tee -a /var/spool/cron/crontabs/$USER
* * * * * /usr/local/bin/docker-registry-remove-empty-repo.sh
EOM
systemctl restart cron
after that, all is ok, if you see the following system log
# tail -f /var/log/syslog
Jun 7 02:46:01 localhost CRON[38764]: (root) CMD (/usr/local/bin/docker-registry-remove-empty-repo.sh)
Jun 7 02:46:01 localhost CRON[38763]: (CRON) info (No MTA installed, discarding output)
Jun 7 02:47:01 localhost CRON[38774]: (root) CMD (/usr/local/bin/docker-registry-remove-empty-repo.sh)
Jun 7 02:47:01 localhost CRON[38773]: (CRON) info (No MTA installed, discarding output)
Jun 7 02:48:01 localhost CRON[38795]: (root) CMD (/usr/local/bin/docker-registry-remove-empty-repo.sh)
Jun 7 02:48:01 localhost CRON[38794]: (CRON) info (No MTA installed, discarding output)
Jun 7 02:49:01 localhost CRON[38804]: (root) CMD (/usr/local/bin/docker-registry-remove-empty-repo.sh)
Jun 7 02:49:02 localhost CRON[38803]: (CRON) info (No MTA installed, discarding output)
Jun 7 02:50:01 localhost CRON[38814]: (root) CMD (/usr/local/bin/docker-registry-remove-empty-repo.sh)
Jun 7 02:50:01 localhost CRON[38813]: (CRON) info (No MTA installed, discarding output)
^C
#