CLI Cheat Sheet#

Docker#

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

Show me#

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

Start#

Start one docker image in detached mode.#
docker run -d <container-id>
Start several containers with docker compose.#
docker-compose -c <docker-compose filename>
Start a docker swarm.#
docker stack deploy -c  <docker-compose filename> <stack name>

Stop#

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

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; ensure 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]"

Random Password#

Generating a random password, you can modify the password length or use the generated password’s first n characters if you don’t want such a long password. Using a password manager saves you from having to memorise them.

Password lengths demonstrated are 32 characters long.

date +%s | sha256sum | base64 | head -c 32 ; echo
< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-32};echo;
# If openssl rand function is installed on your system.
openssl rand -base64 32

Environment Variables#

As <root> create /etc/profile.d/<new-env-file>.sh

This file is for system-wide environment variable settings. It is not a script file but instead consists of assignment expressions, one per line.

Files with the .sh extension in the /etc/profile.d directory gets executed with a new bash login shell. E.G. when logging in from the console or over ssh, and by the DisplayManager when the desktop session loads.

Open the file with an editor as <root>.#
vi /etc/profile.d/<new-env-file>.sh
Add the environment variables.#
export DJANGO_SECRET_KEY=your-secret-django-key
export POSTGRES_USER=your-postgress-user
Use unset#
unset DJANGO_SECRET_KEY
unset POSTGRES_USER

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.

See Environment Variables for more information.

export SECRET_KEY="YOUR_SECRET_KEY"

See here for a tutorial about creating environment variables.

Footnotes