docker: clean up after yourself

After having fun spinning up containers

$ sudo docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                     NAMES
7421735b919f        nginx               "/docker-entrypoint.…"   6 seconds ago       Up 3 seconds        0.0.0.0:8085->80/tcp      nginx5
8fde0b3948f2        nginx               "/docker-entrypoint.…"   16 seconds ago      Up 14 seconds       0.0.0.0:8084->80/tcp      nginx4
fb138477dbd6        nginx               "/docker-entrypoint.…"   45 seconds ago      Up 43 seconds       0.0.0.0:8080->80/tcp      nginx2
b88627f05581        mongo:3.4           "docker-entrypoint.s…"   22 minutes ago      Up 22 minutes       0.0.0.0:8081->27017/tcp   mongo

time to clean up by stopping the container, then deleting it

$ sudo docker container stop 7421735b919f
7421735b919f

Notice its not listed any more

$ sudo docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                     NAMES
8fde0b3948f2        nginx               "/docker-entrypoint.…"   2 minutes ago       Up 2 minutes        0.0.0.0:8084->80/tcp      nginx4
fb138477dbd6        nginx               "/docker-entrypoint.…"   3 minutes ago       Up 3 minutes        0.0.0.0:8080->80/tcp      nginx2
b88627f05581        mongo:3.4           "docker-entrypoint.s…"   24 minutes ago      Up 24 minutes       0.0.0.0:8081->27017/tcp   mongo

You can stop multiple containers at once

$ sudo docker stop 8f fb 
8f
fb

$ sudo docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                     NAMES
b88627f05581        mongo:3.4           "docker-entrypoint.s…"   26 minutes ago      Up 26 minutes       0.0.0.0:8081->27017/tcp   mongo

Remember each of these containers takes up disk space. System prune is aggressive and will delete all containers.

$ sudo docker system prune 
WARNING! This will remove:
  - all stopped containers
  - all networks not used by at least one container
  - all dangling images
  - all dangling build cache

Are you sure you want to continue? [y/N] y
Deleted Containers:
7421735b919f7bd1e5c71daa323c44a811df500561336b4969646587665a4693
8fde0b3948f2239849ab1199e7c858b0a69770d6220f464d1fa9bb459e1c4802
549271b5de3c468ee9e2ab5ceb9048e8ed6f9563ffa0dc2b19d4b4707ef7be31
fb138477dbd6a5f782da41cb21bebc29ed1c2bd00e4a16a652c28a21d6988294
9fd210ce59f2965b5553ccfdb00201b0fa62b7f012d61d5464805a3fceb15f32

Total reclaimed space: 4.456kB

But you did reclaim 4mb! And this is just and example…think in scale.

$ sudo docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                     NAMES
b88627f05581        mongo:3.4           "docker-entrypoint.s…"   30 minutes ago      Up 30 minutes       0.0.0.0:8081->27017/tcp   mongo

RELATED POST

Veritas Volume Manager: Growing a disk group and expand the filesystem

Validated by Mr Man! Lets start off on node2 [root@node02 ~]# vxdisk list DEVICE TYPE DISK GROUP STATUS sda auto:none…

Virtual Machine Manager: Error starting domain

Starting up the KVM error occurred Error starting domain: Requested operation is not valid: network 'default' is not active Locate…

Git Commands

How to initialize a Git repo: Everything starts from here. The first step is to initialize a new Git repo…

Lab Hack: Raspberry Pi running VMWare ESXi

As strange as the title sounds, yes I am running VMWare ESXi on a Raspberry Pi 4 Model B (4GB)…