Monday, May 17, 2021

Cleaning docker overlay2 disk usage

After using docker for a while, one thing that I notice is, the disk space usage on /var/lib/docker/overlay2 is quite high. For jsut 3 running containers, and a couple of images, my overlay2 disk usage is around 30GB, which is quite high. To reclaim back the space, what we can do is to clear off unused containers, images, volumes and other docker components, but that is going to be a daunting task for some.

Fortunately, docker comes with some tools that can ease up our work maintaining the software. The command to run it as per below:

$ docker system prune --all --volumes

This command will clear all unused components in docker, including unused volumes and images, Rest assured that running containers, and images used by running containers will be spared. From the man page, we can see that this command's usage is for cleaning up unused data in docker.

Sunday, May 16, 2021

Downloading torrent using command line

To download torrents using a command line, the easiest tool to use is transmission-cli. This tool is available in debian based distro and redhat based distro alike.

To use this tool, you have to install it first.

In debian based distro:

$ sudo apt install transmission-cli -y

In redhat based distro:

$ sudo yum install transmission-cli -y 

To use it with torrent file, first download the torrent file, and then run transmission-cli against the file

$ wget https://releases.ubuntu.com/20.04/ubuntu-20.04.2-live-server-amd64.iso.torrent

$ transmission-cli ubuntu-20.04.2-live-server-amd64.iso.torrent


To use it with magnet link, just run transmission-cli against the magnet link

$ transmission-cli magnet:?xt=urn:btih:eb6354d8d9b9427458af8bee90457101a4c1e8e3&dn=archlinux-2021.05.01-x86_64.iso


Saturday, May 8, 2021

Listing docker containers according to fields

Command like 'docker ps' is a good tool to check on your running container/s. This is visually pleasant if you do not have many containers. What if you have hundreds of containers, and you just want to print just the names of the containers, or even better the names and id of the containers?

We can use --format flag in this situation. This flag is available on pretty much every docker commands that produce some kind of output to stdout, so that you can filter what you want to see as the output.

To use this flag, you just need to follow below format:
$ docker ps --format '{{json .Names}}'

"hardcore_carson" 


whereby ".Name" is the field that you want to be displayed. For example, you want to list out just the ID of all the running containers, you can use:
$ docker ps --format '{{json .ID}}'

"e914bd4963d4" 


You can see that the field is different from the displayed field name without the --format flag.
$ docker ps
CONTAINER ID   IMAGE     COMMAND     CREATED          STATUS          PORTS     NAMES
e914bd4963d4   alpine    "/bin/sh"   29 minutes ago   Up 29 minutes             hardcore_carson

To know which flag is available to be used:
$ docker ps --format '{{json .}}'

{"Command":"\"/bin/sh\"","CreatedAt":"2021-05-08 11:32:12 +0800 +08","ID":"e914bd4963d4","Image":"alpine","Labels":"","LocalVolumes":"0","Mounts":"","Names":"hardcore_carson","Networks":"bridge","Ports":"","RunningFor":"33 minutes ago","Size":"0B (virtual 5.61MB)","State":"running","Status":"Up 33 minutes"} 


To list 2 (or more) fields:
$ docker ps --format '{{json .ID}} {{json .Names}}'

"e914bd4963d4" "hardcore_carson" 


You can also use the --format without the json keyword, the only different is the output would not be double quoted (which is not easy on the eyes if you have many fields)

$ docker ps --format '{{.ID}} {{.Names}}'

e914bd4963d4 hardcore_carson