The following is a compilation of common tasks someone running Docker would likely need to know about.
Listing Running Containers
The docker ps command will output a list all of running containers. The output is formatted as a table with columns that provide brief details about each container, such as ID, name, network ports, etc.
Listing All Containers, including Exited Ones
Running the docker ps command alone will only list running containers. To view all containers, including those that have already exited, we use the -a flag. Similar to listing all running containers, this will output a table providing summary information about the containers.
docker ps -a
Stopping All Running Containers
Sometimes you need to quickly stop all running containers. Docker only provides a mechanism to stop individual containers, one at a time. By piping a list of all running container ID’s to docker stop, we can effectively stop anything that is running.
docker ps -q | xargs docker stop
List Only Exited Containers
Sometimes you only care about viewing a list of exited containers. To do so we can filter our output using the -f flag. The search term for viewing exited containers is status=exited.
docker ps -f status=exited
Remove All Containers
Docker doesn’t provide a command to remove all containers. However, we can output a list of container IDs and pipe that into a docker rm command. Using the -a flag to list all containers and the -q to output only their IDs, we can pipe the results into docker rm to remove all of them.
docker ps -a -q | xargs docker rm
Remove Only Exited Containers
Removing all old containers should be done on a regular basis. Using the -a flag to list all containers and then the -f flag to narrow our search to only exited containers, we can output the results to docker rm.
For learning how to perform this task on a regular basis, view our tutorial Cleaning Up Your Old Docker Containers.
docker ps -a -q -f status=exited | xargs docker rm
List All Images
List of the images you have stored on your local system.
Remove All Images
Remove all images stored locally
docker images -q | xargs docker rmi
Shell Into Running Container
Troubleshooting a container can be difficult if the required information isn’t being outputted to STDOUT. To diagnose a problem with your container you can just shell into it using bash. The following command will shell into a running container.
docker exec -it 14eba26408ed bash --
Running Container File Sizes
To quickly see the storage size of your running containers, use the ps command with the -s flag.
docker ps -s