CLI Cheat Sheet#
Docker#
A selection of useful Docker CLI commands, with copy button convenience.
Show me#
docker container ls
docker image ls
docker network ls
docker volume ls
docker ps
Start#
docker run -d <container-id>
docker-compose -c <docker-compose filename>
docker stack deploy -c <docker-compose filename> <stack name>
Stop#
docker stop <container-id>
docker stop $(docker ps -a -q)
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.
docker container prune
docker image prune
docker image prune -a
docker network prune
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
andCLOSED
.
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.
vi /etc/profile.d/<new-env-file>.sh
export DJANGO_SECRET_KEY=your-secret-django-key
export POSTGRES_USER=your-postgress-user
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