This is a collection of useful commands I have found over time.
Update containers using
docker-compose
# Update your tags inside docker-compose file
# This method will ensure that everything with the new images is okay
# and then update the actual running containers. (avoid downtime)
# `start` and `restart` will NOT work
> docker-compose build
> docker-compose up -d
Find the ip address of a container
> docker inspect container_name | grep '"IPAddress"'
Use the same
docker network
for all the containers inside adocker-compose
file
# This will be extremely helpful for using docker-compose in a modular manner.
# Multiple compose files can be used instead of one, without any extra modification, while allowing
# containers of different files to directly communicate with each other.
# Firstly, create the docker network "yourNetworkName"
> docker network create yourNetworkName
# Secondly, add the following lines to your docker-compose.yml and you're good to go.
networks:
default:
external:
name: yourNetworkName
Stop all the active containers at once
> docker stop $(docker ps -q)
Login inside a
docker container
withadmin
rights, and aninteractive terminal
> docker exec -u 0 -it container_name /bin/bash
Alternatively, you can use
sh
inside the container
> docker exec -u 0 -it container_name /bin/sh
Get web
port
usage of every service using it
> sudo lsof -itcp
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
docker-pr 20887 root 4u IPv6 225535203 0t0 TCP *:1883 (LISTEN)
Generate strong
ssh key
for server setup
> ssh-keygen -t rsa -b 4096
Show
files size
in a directory andsort by size
> du -csh * | sort -rh
# du: Disk Usage
# -s: Summary for each file
# -h: Human readable format
# -c: Produce grand total
> du -csh * | sort -rh | head -n10
# head -n10: Reduces the output to only 10 rows for readability
Convert windows line endings to unix
dos2unix file.txt
Change
ownership
of all files on current directory tocurrent user
> chown -R $USER:$USER .
# -R: Do it recursively.
# .: Do it for everything inside the current directory.
# $USER: The user currently logged in.
Create 'PDF' by merging images, text files and pdfs
# sudo apt install imagemagick
> convert image1.jpg image2.png text.txt PDFfile.pdf outputFileName.pdf
Compress PDF files using ghostscript
> gs -sDEVICE=pdfwrite \
-dCompatibilityLevel=1.4 \
-dPDFSETTINGS=/ebook \
-dNOPAUSE \
-dQUIET \
-dBATCH \
-sOutputFile=output.pdf \
input.pdf
# Additional options: http://milan.kupcevic.net/ghostscript-ps-pdf/
Do anything PDF related using a nice GUI. (not a command but hey)
> pdfsam
Run something from a terminal and detach it from it.
> thingToRun &
> disown
# All you have to do is type & after whatever you want to run.
# After the command runs, your terminal will seem detached, but if you exit, your application will be killed.
# So you just type disown, and profit! you can exit your terminal.
Count number of lines and words inside a file or console output.
input | wc -lw
# -l: Count number of lines
# -w: Count number of words