Friday, 10 June 2016

Anatomy of the Docker for Mac virtual disk

Experimenting with Docker for Mac (beta) I wanted to look inside the virtual machine running Docker and reclaim unused disk space.

First of all -- it stores the virtual disk under ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/Docker.qcow2.

In the next step I have copied this file into the folder where a Vagrant Linux VM run, listed content, found a running Docker container and shrunk virtual disk:
# Show some info about the virtual disk
mymac> qemu-img check ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/Docker.qcow2  ✓  10118
No errors were found on the image.
54968/1048576 = 5.24% allocated, 0.95% fragmented, 0.00% compressed clusters
Image end offset: 3606183936
mymac> qemu-img info ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/Docker.qcow2  ✓  10138
image: ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/Docker.qcow2
file format: qcow2
virtual size: 64G (68719476736 bytes)
disk size: 3.4G
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: true
refcount bits: 16
corrupt: false
# Copy and compress virtual disk to Linux VM folder
qemu-img convert -c -O qcow2 ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/Docker.qcow2 dockermac.qcow2
# Login into Linux VM
mymac>vagrant ssh
[vagrant@centos7-adb ~]$ ll /vagrant
total 1065220
-rw-r--r--. 1 qemu qemu 1090781184 Jun 10 12:33 dockermac.qcow2
-rw-r--r--. 1 vagrant vagrant 2137 Jun 10 12:08 Vagrantfile
# Install libguestfs
[vagrant@centos7-adb ~]$ sudo yum install libguestfs-tools
# Start libguestfs shell
[vagrant@centos7-adb ~]$ sudo guestfish
><fs> add /vagrant/dockermac.qcow2 # add a copy of Docker for Mac virtual disk
><fs> run
100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00
><fs> list-filesystems
/dev/sda1: swap
/dev/sda2: ext4
><fs> mount /dev/sda2 / # mount virtual disk under Linux VM
><fs> ll /
total 52
drwxr-xr-x 10 root root 4096 Jun 9 13:29 .
drwxr-xr-x 19 root root 4096 Jun 10 16:17 ..
drwxr-xr-x 4 root root 4096 May 20 20:54 cache
drwxr-xr-x 2 root root 4096 May 6 06:00 empty
drwxr-xr-x 2 root root 4096 May 20 20:54 git
drwxr-xr-x 8 root root 4096 Jun 9 13:29 lib
lrwxrwxrwx 1 root root 9 Jun 9 13:29 lock -> /run/lock
drwxr-xr-x 3 root root 4096 Jun 9 13:29 log
drwx------ 2 root root 16384 Jun 9 13:29 lost+found
lrwxrwxrwx 1 root root 4 Jun 9 13:29 run -> /run
drwxr-xr-x 3 root root 4096 May 6 06:00 spool
drwxrwxrwt 2 root root 4096 May 6 06:00 tmp
---------- 1 root root 0 Jun 9 13:29 transfused_start.log
# found a container running in Mac
><fs> ll /lib/docker/containers/45a946cb64d29f7aea8789c710093d494ae07d6bb9dc256ddb5e70e1992be95d/
total 1016
drwx------ 3 root root 4096 Jun 9 14:14 .
drwx------ 3 root root 4096 Jun 10 09:46 ..
-rw-r----- 1 root root 1000621 Jun 10 10:06 45a946cb64d29f7aea8789c710093d494ae07d6bb9dc256ddb5e70e1992be95d-json.log
-rw-r--r-- 1 root root 3337 Jun 9 14:14 config.v2.json
-rw-r--r-- 1 root root 1101 Jun 9 14:14 hostconfig.json
-rw-r--r-- 1 root root 13 Jun 9 14:14 hostname
-rw-r--r-- 1 root root 174 Jun 9 14:14 hosts
-rw-r--r-- 1 root root 37 Jun 9 14:14 resolv.conf
-rw------- 1 root root 71 Jun 9 14:14 resolv.conf.hash
drwx------ 2 root root 4096 Jun 9 14:14 shm
[vagrant@centos7-adb ~]$ exit
# Now if you want to shrink virtual disk to save space (e.g. after you cleaned up obsolete images and containers)
# Stop running Docker daemon
mymac> cd ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/
mymac> mv Docker.qcow2 Docker.qcow2.backup
mymac> qemu-img convert -O qcow2 Docker.qcow2.backup Docker.qcow2
mymac> qemu-img check Docker.qcow2.backup  ✓  10162
No errors were found on the image.
54969/1048576 = 5.24% allocated, 0.96% fragmented, 0.00% compressed clusters
Image end offset: 3606249472
mymac> qemu-img check Docker.qcow2  ✓  10163
No errors were found on the image.
39636/1048576 = 3.78% allocated, 0.01% fragmented, 0.00% compressed clusters
Image end offset: 2601385984
# Start your Docker for mac
mymac> /Applications/Docker.app/Contents/Resources/bin/docker-diagnose  ✓  10165
OS X: version 10.11.5 (build: 15F34)
Docker.app: version: v1.11.1-beta14.5.m
Running diagnostic tests:
[OK] docker-cli
[OK] Moby booted
[OK] driver.amd64-linux
[OK] vmnetd
[OK] osxfs
[OK] db
[OK] slirp
[OK] menubar
[OK] environment
[OK] Docker
[OK] VT-x
Docker logs are being collected into /tmp/20160610-205107.tar.gz
Most specific failure is: No error was detected
# All good!
view raw VM session.sh hosted with ❤ by GitHub

Not a lot of science, nevertheless -- learned a new tool and now know where to search and what to do if my local Docker is getting occupy too much disk space.

PS: Special thanks to Richard WM Jones for useful tips about guestfs tools and James Coyle for an article Reclaim disk space from a sparse image file (qcow2/vmdk),

1 comment:

  1. Thanks for sharing such an informative Article. It will be beneficial to those who seek information. Continue to share your knowledge through articles like these, and keep posting on

    Data Engineering Solutions 

    AI Solutions

    ReplyDelete