Stop and remove images using docker compose down --rmi all
In the previous steps, we learned how to stop and remove containers, networks, and volumes using docker compose down
. By default, docker compose down
does not remove the Docker images that were used to create the containers. In this step, we will learn how to remove images using the --rmi all
flag with docker compose down
.
First, let's bring up the services again. Make sure you are in the ~/project/my-compose-app
directory.
cd ~/project/my-compose-app
Run the docker compose up -d
command to start the services in detached mode.
docker-compose up -d
This will recreate the containers and the network.
Now, let's check the Docker images on your system using the docker images
command.
docker images
You should see the nginx
and redis
images listed, along with any other images you might have on your system.
Now, let's stop and remove the services, network, volumes, and also the images using docker compose down --rmi all
.
docker-compose down --rmi all -v
We are including the -v
flag again to ensure volumes are also removed. The --rmi all
flag tells Docker Compose to remove all images used by any service in the docker-compose.yml
file, even if they are not tagged.
You should see output indicating that the containers, network, volumes, and images are being stopped and removed.
To verify that the containers have been removed, use docker ps -a
.
docker ps -a
The web
and redis
containers should not be listed.
To verify that the network has been removed, use docker network ls
.
docker network ls
The my-compose-app_app-network
should not be listed.
To verify that the volumes have been removed, use docker volume ls
.
docker volume ls
Any volumes related to this project should not be listed.
Finally, to verify that the images have been removed, use docker images
.
docker images
The nginx
and redis
images that were used by the services should no longer be listed in the output, unless they are being used by other containers or have other tags. docker compose down --rmi all
removes images that are not used by any other container.