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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# Show some info about the virtual disk mymac> qemu-img check ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/Docker.qcow2 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 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 > add /vagrant/dockermac.qcow2 # add a copy of Docker for Mac virtual disk > run 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00 > list-filesystems /dev/sda1: swap /dev/sda2: ext4 > mount /dev/sda2 / # mount virtual disk under Linux VM > 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 >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 ✓ 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 ✓ 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! |
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)