Skip to content

Instantly share code, notes, and snippets.

@yves-vogl
Forked from sfwn/0 gc-step.md
Created May 5, 2023 12:48
Show Gist options
  • Save yves-vogl/55f6aa4e7b6af9e2264a1e38e0b6b3d5 to your computer and use it in GitHub Desktop.
Save yves-vogl/55f6aa4e7b6af9e2264a1e38e0b6b3d5 to your computer and use it in GitHub Desktop.
docker registry cleanup script : just reserve most recently 5 images per repo

you need jq first.

Steps:

  1. download the script && chmod +x
  2. enable "delete" inside docker registry container
/etc/docker/registry/config.yml

storage:
  delete:
    enabled: true
  1. restart registry container
  2. exec script (note: you can use 2>&1 | tee to output the result to both a file and standard output)
  3. docker exec -it {registry-id} /bin/registry garbage-collect /etc/docker/registry/config.yml
  4. stop registry container && start registry container
#!/bin/bash
LIVINGDAYS=${1-150}
DOCKER_REGISTRY="localhost:5000/v2"
ACCEPT_HEADER="Accept: application/vnd.docker.distribution.manifest.v2+json"
function get_repositories {
curl -Ls "${DOCKER_REGISTRY}"/_catalog?n=10000 | jq -r '."repositories"[]'
}
function get_repository_tags {
curl -Ls "${DOCKER_REGISTRY}"/"$1"/tags/list?n=10000 | jq -r '."tags"[]' | grep "$2" | sort -r | tail -n +6
}
function get_tag_digest {
REPOSITORY="$1"
TAG="$2"
curl -ILs --header "${ACCEPT_HEADER}" "${DOCKER_REGISTRY}"/"${REPOSITORY}"/manifests/"${TAG}" | grep Docker-Content-Digest | awk '{print $2}' | tr -d '\r'
}
REPORITORIES=$(get_repositories)
echo ALL REPOS: ${REPORITORIES}
echo
for REPOSITORY in ${REPORITORIES[@]}
do
ENVS=("_PRO_" "_PRE_" "_TEST_" "_DEV_")
for env in ${ENVS[@]}
do
TAGS=$(get_repository_tags "${REPOSITORY}" "$env")
echo ${REPOSITORY}: ${TAGS}
echo
for TAG in ${TAGS[@]}
do
echo ${REPOSITORY}:${TAG}
DIGEST=$(get_tag_digest "${REPOSITORY}" "${TAG}")
echo "${DOCKER_REGISTRY}"/"${REPOSITORY}"/manifests/"${DIGEST}"
URL="${DOCKER_REGISTRY}"/"${REPOSITORY}"/manifests/"${DIGEST}"
curl -s -X DELETE -i $URL
echo -------------------------------------------------------------------------------------
echo
done
done
done
$ crontab -e
0 3 * * * bash /root/docker-reg-gc/cron-docker-reg-gc.sh 2>&1 |tee /root/docker-reg-gc/log/$(date +\%Y-\%m-\%d).log

Note the backslash escaping the % sign. Reference

if you found errors in /var/log/message like this,

crond: sendmail: fatal: parameter inet_interfaces: no local interface found for ::1

maybe you should Disable IPV6 in linux distro and config your postfix to use only ipv4.

if you cannot receive mails from cron, please start postfix similar to systemctl start postfix

#!/bin/sh
check() {
if [[ $? != 0 ]]; then
npm i nodemailer optimist fs
node mail.js $(date +%Y-%m-%d)
exit 1
fi
}
mkdir -p /root/docker-reg-gc/
cd /root/docker-reg-gc/
echo "exec docker-reg-gc ..."
bash docker-reg-gc.sh; check
echo "exec docker-reg-gc done"
echo "exec garbage-collect command ..."
docker exec registry2 /bin/registry garbage-collect /etc/docker/registry/config.yml; check
echo "exec garbage-collect command done"
echo "restarting registry ..."
docker stop registry2 && docker start registry2; check
echo "restarting registry done"
echo "all done"
'use strict';
const nodemailer = require('nodemailer');
const fs = require('fs')
const argv = require('optimist').argv;
const logfile = '/root/docker-reg-gc/log/' + argv._[0] + '.log';
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
service: 'QQex',
auth: {
user: 'you@email',
pass: 'youpassword'
}
});
// setup email data with unicode symbols
let mailOptions = {
from: 'sendfrom@email', // sender address
to: ['to@email1', 'to@email2'], // list of receivers
subject: 'ERROR: docker registry crontab error', // Subject line
text: fs.readFileSync(logfile) // plain text body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message %s sent: %s', info.messageId, info.response);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment