In this guide, you will learn how to remove images in Docker, a platform designed to run applications in container environments.
Docker images are files used to execute code in a Docker container. They act as templates for containers. You can think of them as similar to snapshots in virtual machine (VM) environments.
How to Remove Images in Docker
Get Image Details
To remove an image, you need to find the specific ID you want to delete. Run the following command to grab this information:
docker images -a
Remove Single Image
After you find the specific image you want to remove, execute the following command.
docker rmi <image-id>
Remove Multiple Images
If you want to remove multiple images at once, simply append the image ID to the previous command with a space between, like so.
docker rmi <image-id> <image-id> ...
Remove All Images
You can use a single command to remove all images from the docker container at once. In this example, $() is the shell syntax, returning what is executed inside. Furthermore, -q is an option that returns the unique image IDs.
docker rmi $(docker images -q)
How to Remove Containers in Docker
Get a Container
To remove a container, you need to find the specific ID you want to delete. Run the following command to grab this information:
docker ps -a
Stop Running Containers
Docker needs to stop all containers running before they can be deleted. To do so, run the following command:
docker stop $(docker ps -a -q)
Delete All Stopped Containers
docker rm $(docker ps -a -q)