docker prune equivalent command in Kubernetes

2/22/2022

I have two below docker commands, can someone let me know the equivalent kubernetes (kubectl) command for single node cluster :

docker image prune -a --force --filter until=5m
docker container prune --force --filter until=5m

Also are these two commands doing same thing ? I understand that first one is deleting image and second one is deleting container, but I am not sure about extra params like -a , --force and --filter are doing.

-- solveit
docker
docker-swarm
k3s
kubectl
kubernetes

2 Answers

2/22/2022

The pod is the min unit managed by k8s, so you can use kubectl delete to remove a pod, not containers.

You can also use docker image prune to remove images if your k8s manage the docker containers, not others.

-- WENJUN CHI
Source: StackOverflow

2/22/2022

can someone let me know the equivalent kubernetes (kubectl) command for single node cluster

docker image prune -a --force --filter until=5m
docker container prune --force --filter until=5m

You don't directly interact with the docker containers or images on the nodes themselves, and you shouldn't have to. There is no direct equivalent on kubectl. If you wanted to run a prune on the nodes to (for example), save space, then you could setup a cronjob on the node itself to do this -- Kubernetes does not support this functionality.

If you are trying to do this to force Kubernetes to pull a new version of the image and not used the cached version, then you can just use the imagePullPolicy directive (documentation) to force Kubernetes to attempt to pull a new version each time

What are you trying to achieve by pruning the images on the nodes?

Also are these two commands doing same thing ?

No, they are not doing the same thing. Docker works with images. You build or pull an image, and to run it, you create a container. You can have multiple containers that run the same image, but you can only have one of that image.

--force will no prompt for confirmation before deleting (so the usual "be careful" warning applies here)

-a or --all will remove all unused images, not just dangling ones. This answer explains the difference between a dangling and unused image.

You can also use the --help switch to get details on what the commands do and their accepted options:

$ docker image prune --help

Usage:  docker image prune [OPTIONS]

Remove unused images

Options:
  -a, --all             Remove all unused images, not just dangling ones
      --filter filter   Provide filter values (e.g. 'until=<timestamp>')
  -f, --force           Do not prompt for confirmation
$ docker container prune --help

Usage:  docker container prune [OPTIONS]

Remove all stopped containers

Options:
      --filter filter   Provide filter values (e.g. 'until=<timestamp>')
  -f, --force           Do not prompt for confirmation
-- Blender Fox
Source: StackOverflow