CLI Cheat Sheet#

Docker#

A selection of useful Docker CLI commands, with copy button convenience.

Show me#

List all containers.#
docker containers
List all images.#
docker image ls
List all networks.#
docker network ls
List all volumes.#
docker volume ls
List all running containers.#
docker ps

Stop#

Stop one running container.#
docker stop <container-id>
Stop all running containers.#
docker stop $(docker ps -a -q)

Prune / Delete#

Docker takes a conservative approach to remove unused objects. Items such as images, containers, volumes, and networks are generally not removed unless you explicitly ask Docker to do so. The result is Docker uses extra disk space.

To solve this problem, Docker provides a prune command for each type of object. Docker also provides docker system prune [1] to clean up multiple types of objects at once.

See a selection of the most commonly used Docker commands for the development of the django-cookiecutter project below.


WARNING! This will remove all stopped containers.#
docker container prune
WARNING! This will remove all dangling images.#
docker image prune
WARNING! This will remove all images without at least one container associated to them.#
docker image prune -a
WARNING! This will remove all networks not used by at least one container.#
docker network prune
WARNING! This will remove all volumes not used by at least one container.#
docker volume prune

One command to rule them all…

Suppose you want to do a major cleanup with one command.

Ensure nothing happens to your production stack; make sure your production stacks are running.

Warning

WARNING! This will remove:

  • all stopped containers.

  • all networks not used by at least one container.

  • all dangling images.

  • all dangling build cache.

docker system prune

Linux-macOS-Windows#

A selection of useful CLI commands, with copy button convenience.

Check Ports#

Useage options: LISTEN, ESTABLISHED and CLOSED.

sudo lsof -i -P -n | grep LISTEN
sudo lsof -i:22 ## see a specific port such as 22 ##
sudo nmap -sTU -O IP-address-Here
sudo ss -tulpn | grep LISTEN

Hint

ss command options.

-t : Show only TCP sockets on Linux.

-u : Display only UDP sockets on Linux.

-l : Show listening sockets. For example, TCP port 22 is opened by SSHD server.

-p : List process name that opened sockets.

-n : Don’t resolve service names i.e. don’t use DNS.

Useage options: LISTEN, ESTABLISHED and CLOSED.

sudo lsof -i -P -n | grep LISTEN
netstat -a -n | grep 'LISTEN '

Hint

command options.

-i : for IPv4 and IPv6 protocols.

-p : Display raw port number, not resolved names like ftp, http.

-P : Omit the port names.

-n : Don’t resolve service names i.e. don’t use DNS.

-a : [Netstat] Show all sockets.

-n : [Netstat] Don’t resolve service names i.e. don’t use DNS.

netstat -bano | more
netstat -bano | grep LISTENING
netstat -bano | findstr /R /C:"[LISTEING]"

Django Secret Key#

To generate a Django secret key, in your favourite CLI paste the code below and hit enter.

python -c 'from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())'

Copy the key generated and exit the python shell.

Caution

If you are storing your secret key in an environment variable.

In linux style OS the secret key must be enclosed in ” “.

If not an error may be raised and the key may not save.

export SECRET_KEY="YOUR_SECRET_KEY"

See here for a tutorial about creating environment variables.

Footnotes