Docker and caching apt-get for guests on the same host

Mon 02 February 2015
By stu

Docker is great for isolation, you probably still want some caching, the Dockerfile from extellisys is a great start:

https://github.com/extellisys/docker-image-apt-cacher-ng

Running the above, runs apt-cache-ng and exposes it's port as 3142.

This is great for a LAN, if you want to use this for caching on your dev box then read on.

If you just want lines to paste into other Dockerfiles, skip to the end.

To avoid surprises I tend to change the "FROM" part of any Ubuntu or Debian based Dockerfiles to use the same distro as the host (in this case "ubuntu:14.10"), this should save a little space too.

Optionally use the cache with Docker guests on the same host.

Desired result - use the cache if available, otherwise don't.

Avahi is the usual way to do this, unfortunately it doesn't work easily in between host and guest. We can make our own way of doing this.

So all we need to do is find the host IP from another guest -

$ route -n | awk '/^0.0.0.0/ {print $2}'

The other part is optionally using a proxy - the undocumented apt.conf.d setting Acquire::http::ProxyAutoDetect is what we want. It runs a discovery script that either outputs an IP of a proxy, or DIRECT if there is none.

Discovery script and setting

/usr/local/bin/apt-ng-host-discover:

 #!/bin/bash
 HOST_IP=$(route -n | awk '/^0.0.0.0/ {print $2}')
 if nc -w1 -z $HOST_IP 3142; then printf http://$HOST_IP:3142; else printf DIRECT; fi

/etc/apt/apt.conf.d/30proxy:

 Acquire::http::ProxyAutoDetect "/usr/local/bin/apt-ng-host-discover";

Putting it together / tl;dr;

Everything can be munged into 5 lines you paste into your Dockerfile:

 RUN HOST_IP=$(route -n | awk '/^0.0.0.0/ {print $2}');   
echo "#!/bin/bash" > /usr/local/bin/apt-ng-host-discover;   
echo "if nc -w1 -z $HOST_IP 3142; then printf http://$HOST_IP:3142; else printf DIRECT; fi" >> /usr/local/bin/apt-ng-host-discover &&   
chmod +x /usr/local/bin/apt-ng-host-discover
 RUN echo 'Acquire::http::ProxyAutoDetect "/usr/local/bin/apt-ng-host-discover";' > /etc/apt/apt.conf.d/30proxy

Further work

Run the extellisys cache on boot, and disable any other caching ( http://ask.xmodulo.com/disable-apt-cache-debian-ubuntu.html ).