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 image

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

Docker gives the flexibility to remove the 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 can be removed based on condition by using –filter flag. for example, you can remove an image which is created before 50hours ago.

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

So these are all 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.

You May Also Like

Leave a Reply

Your email address will not be published. Required fields are marked *