Docker cheat sheet

My cheat sheet of commonly used docker commands.

List all containers

docker ps --all or docker ps -a

Remove all containers and images

Here are the two Docker commands you can run in sequence to completely remove (delete) all of your containers and images.

To remove all containers:

docker rm $(docker ps -a -q)

To remove all images:

docker rmi $(docker images -q)

Warning: Executing the commands shown above will destroy all your containers and images; it will not be possible to restore them.

List images

docker images

Remove an image

docker rmi <image-id>

Warning: Executing the commands shown above will destroy the given image; it will not be possible to restore it.

Container shell access

The docker exec command allows you to run commands inside a Docker container. The following command line will give you a bash shell inside your container:

docker exec -it <container-name> bash

If the container does not have bash installed you can use the following as an alternative:

docker exec -it <container-name> sh

Type exit to exit out of the container and return to your host shell.

List ports

The port command can be used to display the ports that are exposed by a given container.

docker port <container-name>

List processes

To see the processes running in a container, you can use the top command (similar to running the Linux top command):

docker top <container-name>

View container log file

docker logs <container-name>

Follow container log file

docker logs --follow <container-name>


docker logs -f <container-name>

Stop all running containers

docker stop $(docker ps -a -q)

Inspect container information

docker inspect <container-name-or-id>

This returns verbose information about the container in JSON format.

You can zero in on a particular attribute using the f parameter. The following, for example, will print only the Mounts section of the container information that would otherwise be found in the whole JSON print.

docker inspect -f '{{ .Mounts }}' <container-name-or-id>