Clean Up Old Docker Images in Nexus Docker Repository

Sathish Kumar
4 min readOct 19, 2018

In many cases cleaning up old junks, images, data periodically is very important equal to 99.9% up time of server. Sometime its ok to have some junks and sometime its very critical, having old data/images which may affect the server up time.

In this case its ok, Nexus repository stops working efficiently only after it consumes 98% of disk space. I use docker repository in Nexus, and some docker image is 500mb, and some even goes to 1.5GB. Even the images are compressed and stored as incremental revisions, it may easily lead to disk full in 3–6 months.

Into the topic, How to clean-up old docker images in Nexus Repository?

Install Nexus-CLI

$ sudo wget https://s3.eu-west-2.amazonaws.com/nexus-cli/1.0.0-beta/linux/nexus-cli
$ mv nexus-cli /usr/bin/
$ chmod +x /usr/bin/nexus-cli

Configure Nexus-CLI

root@sais-nexus:/home/sais# nexus-cli configure
Enter Nexus Host: https://nexus.iamsais.com:8081
Enter Nexus Repository Name: docker-private
Enter Nexus Username: admin
Enter Nexus Password: #######

Test the Setup

root@sais-nexus:/home/sais# nexus-cli image ls
root@sais-nexus:/home/sais# nexus-cli image tags -name ubuntu:16.04

Setup Cronjob (nexus-cli) Weekly 12AM Sunday

root@sais-nexus:/home/sais# nano remove-old-docker-image.sh
#!/bin/bash
set -e
nexus-cli image delete -name sais_python -keep 20
nexus-cli image delete -name sais_php -keep 20
nexus-cli image delete -name sais_ubuntu -keep 20
nexus-cli image delete -name sais_magento -keep 20
nexus-cli image delete -name sasi_customer_api -keep 20

Caution

  • This deletes docker images in ascending order, and not by the date. So deleting the docker images with incremental image tags is always safe.
  • You may lose important images if the image tags are not incremental.
  • Be careful when you delete release docker images.

Nexus Create Task — Compact Blob Store — Weekly 12:15 AM Sunday

Nexus -> Login with Admin User -> Server administration and configuration (Settings Icon) -> Tasks

Click “Create Task” -> Select “ Compact blob store”

Create Task Select Type - Compact Blob Store
Create Task Form— Compact Blob Store

Task Enable: Enable or Disable this Task.

Task Name: Name of the task “Compact Blob Store (Nexus CLI)”

Notification Email: Provide your Email. This triggers mail in case of task failure (Optional)

Blob Store: You may have different blob store based on each project or environment. Choose the Blob Store, in my case i choose default.

Task Frequency: Choose based on your Server maintenance schedule. I chose Weekly.

Start Date: Date to start this scheduled task. (There is no end date, you need to disable this task to stop it)

Time to run this task: Choose based on your Server maintenance schedule. I chose 12:15 AM

Day to run this task: Sunday.

Nexus Create Task — Purge Docker Image — Weekly 12:30 AM Sunday

Task Enable: Enable or Disable this Task.

Task Name: Name of the task “Purge Docker images”

Notification Email: Provide your Email. This triggers mail in case of task failure (Optional)

Repository: You may have different repo based on each project or environment. Choose the repository, in my case i choose (All Repositories)

Task Frequency: Choose based on your Server maintenance schedule. I chose Weekly.

Start Date: Date to start this scheduled task. (There is no end date, you need to disable this task to stop it)

Time to run this task: Choose based on your Server maintenance schedule. I chose 12:30 AM

Day to run this task: Sunday.

Nexus Create Task — Compact Blob Store — Weekly 12:45 AM Sunday

Create Compact Blob Store (After Purge Docker Image).

Task Enable: Enable or Disable this Task.

Task Name: Name of the task “Compact Blob Store (After Purge Docker Image)”

Notification Email: Provide your Email. This triggers mail in case of task failure (Optional)

Blob Store: You may have different blob store based on based on each project or environment. Choose a Blob Store, in my case i chose default.

Task Frequency: Choose based on your Server maintenance schedule. I chose Weekly.

Start Date: Date to start this scheduled task. (There is no end date, you need to disable this task to stop it)

Time to run this task: Choose based on your Server maintenance schedule. I chose 12:45 AM

Day to run this task: Sunday.

--

--