Dyego Eugenio/Docker commands to survive

Created Sun, 21 May 2017 21:05:21 +0300 Modified Wed, 03 Aug 2022 08:52:37 +0000

I wrote this collection of useful commands for us! Let’s remember it for eternity.

docker ps –size

An important command which I discovered and use is docker ps --size. Its show what is the size of the container.

[[email protected] ~]$ docker ps --size
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES               SIZE
dbffabfb478a        docker:17.05-dind   "dockerd-entrypoin..."   4 weeks ago         Up About a minute   2375/tcp            docker1705-dind     10.2MB (virtual 110MB)

On this case, you can see the column size which show 10.2MB (virtual 110MB). Compare with the output of docker images:

[[email protected] ~]$ docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker                  17.05-dind          b547d892dffa        4 weeks ago         99.6MB

We discover the virtual size is the sum of image size + 10.2MB (this space was used creating a file with dd command).

The slice (or slices) that compose the base image is not used for each instance that you run with docker, only the the difference will be stored on the hard disk. In this case represented by 10.2MB.

free space used by docker containers and images

[[email protected] ~]$ docker rm $(docker ps -aq)
[[email protected] ~]$ docker rmi $(docker images -aq)
[[email protected] ~]$ docker volume rm $(docker volume ls -q)

Inspect a container and discover its pid

If you have any PID that you saw using a lot of resource (like CPU or memory), use this command to inspect all running dockers and its respective PID - ID - NAME.

[[email protected] docker]$ docker ps -q | xargs docker inspect --format '{{.State.Pid}}|{{.ID}}|{{.Name}}'