Fixing slow “docker rm”

After using docker and docker-compose for a while, I start noticing that some developers have a very fast response for `docker rm` command. Actually, it is not so significant until you don’t do `docker-compose rm`. When `docker-compose` could spend more than one or two minutes on my laptop (with 4 CPUs, 8GM RAM and Arch Linux on board) while cleaning up all containers. Absolutely same `docker-compose rm` on Ubuntu took less than one second.

$ time docker-compose rm --force
# ...
0.76s user 0.06s system 5% cpu 25.461 total
# it took ~25 seconds to remove 14 containers

I wasn’t able to find a solution through google and even server fault didn’t help (http://serverfault.com/questions/782059/make-docker-containers-deletion-faster-for-arch-linux), which is strange because more and more people use docker on local machines for development needs and more people should face this issue.

After this search, it became obvious that I should solve this problem by myself and I started the investigation. After some attempts, I found out that the problem was with “devicemapper” storage driver. While Ubuntu stock kernel supported “aufs” file system, Arch stock kernel does not, so docker picked “devicemapper” for Arch Linux, resulting longer deletions.

To solve the problem I had install custom kernel with “aufs” support. Luckily, there is “AUR” for Arch Linux so problem was solved with just one command:

yaourt -S linux-aufs_friendly

This command compiled new kernel and installed to the system. Alternatevely, I could compile my own kernel using patches from https://github.com/sfjro/aufs4-standalone repository.

After rebooting and loading to new kernel docker picked “devicemapper”, despite aufs appeared in the file systems list:

$ cat /proc/filesystems | grep aufs
> nodev aufs

I fixed that by manually adding ` — storage-driver=aufs` option to systemd unit file:

# /usr/lib/systemd/system/docker.service
# add --storage-driver=aufs to ExecStart:
ExecStart=/usr/bin/docker daemon --storage-driver=aufs -H fd://

Reloading systemd units and restarting docker:

$ sudo systemctl daemon-reload
$ sudo systemctl restart docker

After that docker started to use aufs driver and remove containers with decent speed:

$ docker info | grep Storage 
> Storage Driver: aufs
$ time docker-compose rm --force
# ...
0.50s user 0.03s system 95% cpu 0.555 total
# it took 0.5s to remove 14 containers