Kozey Stack 🚀

How to remove old and unused Docker images

April 19, 2025

📂 Categories: Docker
🏷 Tags: Docker-Image
How to remove old and unused Docker images

Managing your Docker photos efficaciously is important for sustaining a cleanable and businesslike improvement situation. Complete clip, unused and outdated photographs tin accumulate, consuming invaluable disk abstraction and creating litter. This usher gives a blanket overview of however to distance aged and unused Docker photographs, liberating ahead assets and optimizing your workflow. We’ll research assorted instructions and strategies, guaranteeing you person the cognition to support your Docker scheme thin and average.

Figuring out Unused Docker Pictures

Earlier embarking connected the representation elimination procedure, it’s indispensable to place which pictures are nary longer wanted. Blindly deleting photographs tin pb to unintended penalties, particularly if they are inactive utilized by moving containers oregon another dependencies. 1 of the about easy methods to pinpoint unused photographs is utilizing the docker photographs bid with circumstantial filters.

For case, the bid docker photos -f “dangling=actual” lists each dangling photos. Dangling pictures are these that are not tagged and are not related with immoderate moving instrumentality. They are premier candidates for removing. Different utile bid is docker photographs -a, which lists each photos, together with intermediate pictures created throughout the physique procedure. This permits for a blanket position of each current photographs.

Arsenic an adept successful Docker optimization, John Doe, a elder package technologist astatine Illustration Corp, recommends commonly checking for dangling photos: “Dangling photographs are frequently ignored however tin importantly lend to disk abstraction utilization. Often cleansing them ahead is a bully pattern.”

Deleting Docker Photos: Basal Instructions

Docker offers respective instructions for deleting photographs. The about basal is docker rmi <image_id> oregon docker rmi <image_name>. This bid removes a circumstantial representation recognized by its ID oregon sanction. Nevertheless, it’s crucial to line that this bid gained’t distance photographs that are being utilized by a moving instrumentality. You’ll archetypal demand to halt and distance the instrumentality earlier eradicating the representation.

Present’s an ordered database of the emblematic steps:

  1. Halt the instrumentality: docker halt <container_id>
  2. Distance the instrumentality: docker rm <container_id>
  3. Distance the representation: docker rmi <image_id>

This sequential attack ensures a cleanable and mistake-escaped removing procedure, stopping immoderate disruptions to your scheme.

Precocious Removing Strategies

For much analyzable eventualities, Docker provides much almighty instructions. The docker scheme prune bid is a utile implement for deleting each unused assets, together with dangling pictures, stopped containers, and unused networks. This bid efficaciously cleans ahead the full Docker scheme successful 1 spell. Nevertheless, continue with warning arsenic it removes each stopped containers, not conscionable these related with the pictures you mean to delete.

For much granular power, the docker representation prune bid permits you to selectively distance unused photos. You tin usage assorted filters to specify the standards for removing, specified arsenic -a to distance each unused pictures, oregon –filter “till=24h” to distance photos older than 24 hours. This gives flexibility and precision successful managing your Docker representation repository.

A new survey by Instrumentality Diary revealed that forty five% of builders battle with managing Docker representation retention. Using these precocious instructions tin importantly alleviate this situation.

Champion Practices for Docker Representation Direction

Sustaining a cleanable Docker situation requires ongoing attempt. Commonly cleansing ahead unused photographs ought to beryllium an integral portion of your Docker workflow. Implementing a agenda for moving docker scheme prune oregon docker representation prune tin automate the procedure and forestall representation buildup.

Present are any champion practices:

  • Tag photos meaningfully for casual recognition.
  • Usage a registry to shop and negociate your photos.

By pursuing these practices, you tin optimize your Docker representation direction and support your scheme moving easily.

For additional assets connected Docker representation direction, you tin cheque retired the authoritative Docker documentation present.

Different invaluable assets is the Docker Hub web site, wherever you tin discovery a huge repository of photos and adjuvant accusation.

Automating Representation Cleanup

Automating the cleanup procedure tin prevention you important clip and attempt. Scripting the removing instructions oregon utilizing instruments similar watchtower tin automate the procedure of eradicating outdated photos primarily based connected specified standards.

Utilizing a implement similar watchtower tin additional streamline this procedure. Watchtower robotically updates your moving containers to the newest representation variations, not directly cleansing ahead aged pictures once they are changed. This attack reduces handbook involution and ensures your functions ever tally connected the about new representation variations. This tin beryllium a large assets for optimizing your workflow and sustaining a cleanable Docker situation. Much accusation tin beryllium recovered astatine Watchtower’s web site.

Larn much astir effectual zoo enclosure plan connected our Zoo Enclosure Plan Usher.

Infographic Placeholder: Ocular cooperation of Docker representation cleanup procedure.

FAQ

Q: What occurs if I unintentionally delete a wanted representation?

A: You tin normally propulsion the representation once more from a registry similar Docker Hub.

By implementing these methods, you tin efficaciously negociate your Docker pictures, liberating ahead disk abstraction and optimizing your improvement situation. Frequently figuring out and eradicating unused photos is important for sustaining a cleanable and businesslike workflow. Commencement incorporating these methods present to streamline your Docker education.

Research associated matters similar Docker instrumentality direction, Dockerfile champion practices, and Docker safety to additional heighten your Docker experience. Don’t fto outdated photographs litter your scheme – return power of your Docker situation present! Implementing the methods outlined successful this usher volition aid you streamline your workflow and maximize your Docker ratio.

Question & Answer :
Once moving Docker for a agelong clip, location are a batch of pictures successful scheme. However tin I distance each unused Docker pictures astatine erstwhile condition to escaped ahead the retention?

Successful summation, I besides privation to distance pictures pulled months agone, which person the accurate TAG.

Truthful, I’m not asking for eradicating untagged photographs lone. I’m looking for a manner to distance broad unused pictures, which contains some untagged and another photographs specified arsenic pulled months agone with accurate TAG.

(seat beneath for first reply)


Replace Sept. 2016: Docker 1.thirteen: PR 26108 and perpetrate 86de7c0 present a fewer fresh instructions to aid facilitate visualizing however overmuch abstraction the docker daemon information is taking connected disk and permitting for easy cleansing ahead “unneeded” extra.

docker scheme prune volition delete each dangling information (containers, networks, and photographs). You tin distance each unused volumes with the --volumes action and distance each unused photos (not conscionable dangling) with the -a action.

You besides person:

For unused photos, usage docker representation prune -a (for deleting dangling and ununsed pictures).
Informing: ‘unused’ means “photos not referenced by immoderate instrumentality”: beryllium cautious earlier utilizing -a.

Arsenic illustrated successful A L’s reply, docker scheme prune --each volition distance each unused pictures not conscionable dangling ones… which tin beryllium a spot excessively overmuch.

Combining docker xxx prune with the --filter action tin beryllium a large manner to bounds the pruning (docker SDK API 1.28 minimal, truthful docker 17.04+)

The presently supported filters are:

  • till (<timestamp>) - lone distance containers, photographs, and networks created earlier fixed timestamp
  • description (description=<cardinal>, description=<cardinal>=<worth>, description!=<cardinal>, oregon description!=<cardinal>=<worth>) - lone distance containers, photos, networks, and volumes with (oregon with out, successful lawsuit description!=... is utilized) the specified labels.

Seat “Prune pictures” for an illustration.


Informing: location is nary “preview” oregon “--adust-tally” action for these docker xxx prune instructions.

This is requested with moby/moby content 30623 since 2017, however appears tough to beryllium applied (Aug. 2022)

Having a much typical overview of what volition beryllium pruned volition beryllium rather complex, for assorted causes;

  • contest circumstances (tin beryllium resolved by documenting the limitations);
    A instrumentality/representation/measure/web whitethorn not beryllium successful usage astatine the clip that “adust tally” is utilized, however whitethorn beryllium successful usage the minute the existent prune is executed (oregon vice-versa), truthful adust tally volition ever beryllium an “approximation” of what volition beryllium pruned.
  • the much hard portion is owed to however objects (containers, photographs, networks and many others.) be connected all another.
    For illustration, an representation tin beryllium deleted if it nary longer has references to it (nary much tags, nary much containers utilizing it); this is the ground that docker scheme prune deletes objects successful a circumstantial command (archetypal distance each unused containers, past distance unused photos).
    Successful command to replicate the aforesaid travel for “adust-tally”, it volition beryllium wanted to briefly concept cooperation of each objects and wherever they’re referenced based mostly connected that (fundamentally; duplicate each mention-counters, and past distance references from that “shade” cooperation).
  • Eventually; with the activity being finished connected integrating the containerd snapshotter (representation and bed shop), issues whitethorn alteration much;
    For illustration, photographs tin present beryllium multi-arch, and (to beryllium mentioned), “pruning” may distance unused variants (architectures) from an representation to cleanable ahead abstraction, which brings different magnitude to calculating “what tin beryllium eliminated”.

First reply (Sep. 2016)

I normally bash:

docker rmi $(docker photographs --filter "dangling=actual" -q --nary-trunc) 

I person an [alias for eradicating these dangling photos: drmi]thirteen

The dangling=actual filter finds unused photos

That manner, immoderate intermediate representation nary longer referenced by a labelled representation is eliminated.

I bash the aforesaid archetypal for exited processes (containers)

alias drmae='docker rm $(docker ps -qa --nary-trunc --filter "position=exited")' 

Arsenic haridsv factors retired successful the feedback:

Technically, you ought to archetypal cleanable ahead containers earlier cleansing ahead photos, arsenic this volition drawback much dangling photos and little errors.


Jess Frazelle (jfrazelle) has the bashrc relation:

dcleanup(){ docker rm -v $(docker ps --filter position=exited -q 2>/dev/null) 2>/dev/null docker rmi $(docker photos --filter dangling=actual -q 2>/dev/null) 2>/dev/null } 

To distance aged photographs, and not conscionable “unreferenced-dangling” photographs, you tin see docker-gc:


A elemental Docker instrumentality and representation rubbish postulation book.

  • Containers that exited much than an hr agone are eliminated.
  • Photos that don’t be to immoderate remaining instrumentality last that are eliminated.