Episode 10. Deleting Images and Containers
Lots of times it happens we have created multiple containers that are unused or duplicated. It is good practice to remove them from time to time.
A. Deleting Containers:
We can delete docker containers using the below containers:
Method 1 (Using Terminal or Command Prompt):
i. Get container ID to delete
docker ps -a
ii. Delete contained
→ We can delete multiple containers at once.
docker rm <container-id-1> <container-id-2> <container-id-3>
We cannot delete running containers. We need to stop the container first.
iii. Stop the container
docker stop <running-container-id> // Stop container
docker rm <running-container-id> // Remove container
iv. Delete all containers at once:
docker container prune
Method 2 (Microsoft’s Docker extension):
→ This method is applicable if we are using Visual Studio Code editor.
→ Install Docker extension
→ Go to the Docker extension from the left.
→ Here we will see a visual representation of containers and an image
→ Right-click on the container, click Remove
B. Deleting Images:
i. Get the list of images
ii. Deleting Images
→ We can delete multiple images at once such as node, python, PHP, etc.
docker rmi <image-ID>. // Delete singledocker rmi <image-ID-1> <image-ID-2> <image-ID-3> // Delete multiple
NOTE: We cannot delete directly running images. We need to stop all the running containers using the image, then we delete it.
iii. Stop image
docker images // check imagesdocker stop <image-id> // stop imagedocker rmi <image-id> // delete image
iv. Delete images
Delete all images at once
docker image prune
Method 2: (Docker Extension):
In the docker section, we right-click on the image to delete. Click Remove
Deleting unused containers and images is a good way to keep our docker space clean. Try it out, team. Do comment or tweet to me if you face some issue.
Thank you for reading till the end 🙌 . If you enjoyed this article or learned something new, support me by clicking the share button below to reach more people and/or give me a follow on Twitter and subscribe Happy Learnings !! to see some other tips, articles, and things I learn about and share there.