Labs: Basic Docker Commands

Possible for someone to provide me the exact command to type for the 1st lab related to the Basic Docker Commands
Having issue with How to stop all containers at once &
Removing all stopped containers at once
I took a look a the hints & solution, but still could not get it working.
Further, the last question, having same issue: CleanUp:Deleting all Images on Host
Please provide the exact command to type in for this particular questions.
Thank you

Hi @fifafd350,

  • For the question, stop all containers at one & remove all stopped containers.

For this question, you need first to stop all running containers :

docker stop $(docker ps -q)

then we remove stopped containers

docker rm $(docker ps -q)

you can combine both command at once :

docker stop $(docker ps -q) && docker rm $(docker ps -aq) 
  • For the last question :

You need :

  • Stop all containers
  • Delete all stopped containers
  • Delete images as now we don’t have any containers linked to them
docker stop $(docker ps -q)
docker rm $(docker ps -aq)
docker rmi $(docker images -q)

You can combine the three commands like below :

docker stop $(docker ps -q) && docker rm $(docker ps -aq) && docker rmi $(docker images -q)

You need to understand that option -q returns the id of the resource (image, container) and option -a will return all resources

Regard

1 Like

I can provide you with some general commands that might help you with the tasks you mentioned.

To stop all containers at once, you can use the following command:

docker stop $(docker ps -aq)

This command uses docker ps -aq to list all running containers and passes the output to docker stop to stop each container.

To remove all stopped containers at once, you can use the following command:

docker rm $(docker ps -aq)
This command uses docker ps -aq to list all containers (including stopped containers) and passes the output to docker rm to remove each container.

For cleaning up and deleting all images on the host, you can use the following command:

docker rmi $(docker images -aq)
This command uses docker images -aq to list all images and passes the output to docker rmi to remove each image.