docker remove image by tag

Docker remove image by tag. Docker gives a platform to sysadmin and developer to develop, deploy and run the application with an isolated container environment. Docker gives the flexibility to wrap application and services which we can use and run in any environment. in short, you can install your application to one docker container and convert to docker image and use the same image to a different system with ready to use the feature.

In traditional server environment where servers operating system itself use more hardware resource(CPU, Memory, Disk size) and on top of that you install some application which consumes some of the hardware resources, but still server hardware not utilize properly.

Docker gives a feature of a basic operating system with the minimum package installed, which take all the resource from your server kernel. Docker is lightweight so you can even deploy many decker containers within one server. While playing with docker container we need to know some basic command to deploy, remove, push, pull container, you can even try dockerfile to automate docker container deployment which I cover in my last article DockerFile .

To set up the docker environment is quite easy but when you have so many containers and images which serving different applications,  it is very hard to remember which image belongs to which docker container and cleaning images and containers become a nightmare, and on top of that storage space is also needed for container and images growth.   In this article, we will focus some of the useful commands to remove images and Container.

Docker Remove Images

To remove the docker image you need to first list out all downloaded images which you pull from docker hub. to do so run below command with -a flag which shows all available images. docker remove image if you find out any unused images you can run below command it will only work if that image is not bound with any container. If any container is attached or bound with the image first try to stop that then try to remove image.
docker rmi Image

Remove images which having <none> untagged images.

Most of the time when we download images from docker hub it comes with an untagged title. So to handle such case try below commands.

docker rmi $(docker images -aq --filter dangling=true)
docker images -f “dangling=true” -q

or

docker rmi $(docker images | grep "^<none>" | awk '{print $"3"}') --force

If you want to remove docker container you first need to list out the available container and then based on image ID you can remove the container.

docker remove image

Docker remove image by id

Select the image ID which you want to remove and run below command.

Docker remove image by id

docker rm ContainerID

If you want to remove all the running container with the image, first stop all running container then remove by running below command sequence.

docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
docker rmi $(docker images -q)

Force remove all the Docker images.

docker rmi -f $(docker images -f dangling=true -q)

Docker remove image by tag

We can smartly use some tricks to remove the docker image based on the tag for that if your docker image referencing one or more tags then you need to remove one by one before removing the main image. If you try to remove tagged image it will throw error cannot delete image xxxxxx because it is tagged in multiple repositories.
to overcome this either you can untagged all images and then remove or use -f to remove by force by running below command.

docker rmi -f xxxxxxxxx

docker remove conflict

Docker even provides a command to delete all the resource like image, container, network, and volume by running a single command.

docker system prune

You can even filter image and remove based on condition by using –filter flag. for example, you can remove an image which is created before 50 hours ago.

docker image prune -a -f --filter "until=50h"

Docker Remove Images by Pattern

We can filter images by pattern and with the xargs Linux command with -r, –no-run-if-empty flag if there are no arguments, we can remove filtered images at one go.

docker images -a | grep “pattern” | awk ’{print $3}’ | xargs -r docker rmi 

Remove containers using more than one filter.

docker rm $(docker ps -a -f status=exited -f status=created -q) 

Remove Docker dangling images

Dangling images are safe to remove since they are not  dependent or reference by other images. If you have so many in your environment,it can be really pain to remove them, but we can smartly use few docker and Linux commands which help us remove dangling images.

In older versions of Docker, try to  delete dangling images by running below command. 

docker rmi -f $(docker images -f "dangling=true" -q) 

In Docker latest version above command become more simple to run and remember.

 docker image prune

# docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Deleted Images:
[...]

Above command will not only remove dangling images but it will also remove all stopped containers, all networks not used by at least 1 container, all dangling images and build caches and reclaim space.

Pruning images is somewhat dangerous. if you run some applications with the (somewhat oldish) “data container” pattern, prune will happily destroy this container – with your data.

We can even use Docker prune command to delete container, system, network and volume and if you want to bypass warning message by using -f –force flag.

docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y 

Similarly we can use below command.

docker system prune

docker volume prune

docker network prune

Conclusion

So these are all bast possible way to delete docker images and containers. The best way to delete images or container if they are not in use otherwise they simply take space and other resources.It always a good idea to run some cron job which weekly clean your environment, so whenever you have some new task you no need to scratch your head for unwanted things. 

You May Also Like