8

I have imported some docker images to microk8s image cache for local kubernetes deployment using,

microk8s.ctr -n k8s.io image import <docker_image_name>.tar

how can I remove some of the unwanted images from this cache? Is there any command for that?

Thanks in advance!

anju
  • 157
  • 1
  • 2
  • 8

3 Answers3

10

If you want to delete all custom added images from the built-in library, you can do this:

# get all images that start with localhost:32000, output the results into image_ls file
sudo microk8s ctr images ls name~='localhost:32000' | awk {'print $1'} > image_ls 
# loop over file, remove each image
cat image_ls | while read line || [[ -n $line ]];
do
    microk8s ctr images rm $line
done;

Yes, I've used David Castros comment as a base, but since it did not work for Microk8s, I needed to adapt it a bit.

Marco
  • 22,856
  • 9
  • 75
  • 124
7

With --help you can see the that there is a remove option:

> microk8s.ctr -n k8s.io images --help
NAME:
   ctr images - manage images

USAGE:
   ctr images command [command options] [arguments...]

COMMANDS:
     check       check that an image has all content available locally
     export      export an image
     import      import images
     list, ls    list images known to containerd
     pull        pull an image from a remote
     push        push an image to a remote
     remove, rm  remove one or more images by reference
     label       set and clear labels for an image

OPTIONS:
   --help, -h  show help

So you can see the running containers with:

 microk8s.ctr -n k8s.io containers ls

And later remove images with:

 microk8s.ctr -n k8s.io images rm "image_ref"
Matt
  • 7,419
  • 1
  • 11
  • 22
6

It is also possible to do it in one line

microk8s ctr images rm $(microk8s ctr images ls name~='localhost:32000' | awk {'print $1'})
Kamiel Ahmadpour
  • 1,215
  • 12
  • 16