DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • How to Publish Docker Images on Private Nexus Repository Using Jib Maven Plugin
  • How to Publish Docker Images on a Private Nexus Repository Using Jib Maven Plugin
  • How GitHub Codespaces Helps in Reducing Development Setup Time
  • Keep Your Application Secrets Secret

Trending

  • A Software Engineer’s Guide to Thrive in Gen AI Era: Master It or Fade Out
  • Deploy Serverless Lambdas Confidently Using Canary
  • Top NoSQL Databases and Use Cases
  • Modernizing Apache Spark Applications With GenAI: Migrating From Java to Scala
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Cleanup Old Docker Images from Nexus Repository

Cleanup Old Docker Images from Nexus Repository

To keep Docker image management easy, someone built a Nexus CLI to clean up old Docker Images. See how to use it and consider helping out with the project on GitHub.

By 
Mohamed Labouardy user avatar
Mohamed Labouardy
·
Dec. 13, 17 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
15.4K Views

Join the DZone community and get the full member experience.

Join For Free

Many of us are using Nexus as a repository to publish Docker images. Typically, we build images tagged with the commit hash (or using semver ideally) after SCM changes automatically in CI, and we push them to the registry. As a result, there are many "unneeded" and "old" images that, in our case, take up a significant amount of disk space.

I looked around the graphical interface of Nexus, and there's apparently nothing to remove several Docker images at the same time. Or even, a scheduled task to clean up old hosted Docker images — and to also clean up layers that are no longer used by any hosted images.

So I have come up with a simple bash script that uses the Docker Registry API to purge Docker images and keep the last X images and delete all others. But is there a better solution? YES! I built a Nexus CLI.

To install Nexus CLI, find the appropriate package for your system and download it. For Linux:

wget https://s3.eu-west-2.amazonaws.com/nexus-cli/1.0.0-beta/linux/nexus-cli


After downloading Nexus CLI. Add the execution permission to the binary:

chmod +x nexus-cli

Note: For Windows, make sure that the nexus-cli binary is available on the PATH. This page contains instructions for setting the PATH on Windows.

After installing, verify that the installation worked by opening a new terminal session and checking if nexus-cli is available:


Once done, configure the Nexus credentials:

nexus-cli configure

Docker Cleanup in Nexus Repository

Through nexus-cli configure, the Nexus CLI will prompt you for four pieces of information. The Username and Password are your account credentials. You will also need your Nexus Hostname and Docker repository name.

That should be it. Try out the following command from your cmd prompt and, if you have any images, you should see them listed:

Docker Cleanup in Nexus Repository

Display image tags:

nexus-cli image tags -name IMAGE_NAME


Docker Cleanup in Nexus Repository

nexus-cli image info -name IMAGE_NAME -tag TAG

Docker Cleanup in Nexus Repository

To remove a specific image:

nexus-cli image delete -name IMAGE_NAME -tag TAG

Docker Cleanup in Nexus Repository

To keep only the last X images and delete all other:

nexus-cli image delete -name IMAGE_NAME -keep X

Docker Cleanup in Nexus Repository

 That’s it! Let’s go back to Nexus Dashboard:

Docker Cleanup in Nexus Repository

As you can see, Nexus kept only the last four images and deleted the others.

Docker Cleanup in Nexus Repository

The CLI is still in its early stages, so you are welcome to contribute to the project on GitHub.

This article was originally published on labouardy.com

Nexus (standard) Docker (software) Repository (version control)

Published at DZone with permission of Mohamed Labouardy, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How to Publish Docker Images on Private Nexus Repository Using Jib Maven Plugin
  • How to Publish Docker Images on a Private Nexus Repository Using Jib Maven Plugin
  • How GitHub Codespaces Helps in Reducing Development Setup Time
  • Keep Your Application Secrets Secret

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: