Cache deb packages with apt-cacher-ng

I’m playing with Docker and I’m constantly tuning my Dockerfile to install the needed packages.

This continuous refinement forces the build process to download the same deb packages every time and it becomes an actual waste of time and bandwidth.

Solution

apt-cacher-ng is a service which can be run on a Debian or Ubuntu host and will serve as a local cache for deb packages from Debian or Ubuntu repositories.

That’s what I need because I’m on an Ubuntu machine emulating a Debian container.

Host setup

The setup is really easy:

# aptitude install apt-cacher-ng

After the installation I changed the bind address of the service to keep it local (I don’t need LAN exposure), I added my local proxy to reach the internet and I disabled ReuseConnections (see Problems):

root@yoda:/etc/apt-cacher-ng# diff acng.conf acng.conf.old 
28d27
< BindAddress: localhost 172.17.42.1
35d33
< proxy: http://127.0.0.1:5865
322d319
< ReuseConnections: 0

Client setup

On the client side it’s necessary to force apt requests to use the proxy.
In the Dockerfile I added

RUN echo 'Acquire::http::Proxy "http://172.17.42.1:3142";' > /etc/apt/apt.conf.d/90-apt-cacher.conf

which creates the configuration file read by apt-get or aptitude when downloading packages.

Problems

The apt-cacher-ng server seems buggy at least when used in conjunction with a regular proxy. Sometimes I get errors like these on the client side

Err http://http.debian.net/debian/ wheezy/main netbase all 5.0
500  Invalid header

one possible solution is to perform a

# apt-get update

on the host machine (it seems to help). Another useful setting is

ReuseConnections: 0

as explained before.