Tag Archives: network

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.

Git through a proxy

I was connected to a LAN behind a proxy. When I tried to download something from github I got this error:
fatal: unable to connect to github.com:
github.com: Name or service not known

I’m running latest Ubuntu desktop (12.04).

One way I found to bypass this problem is to create a git proxy wrapper.

  1. Create the script i.e. ~/bin/git-wrapper.sh and configure the 2 variables according to your needs. My proxy is a local service which forwards my requests to the real LAN proxy. Local service is configured to listen on the 5865 port of my local (127.0.0.1) machine.
    #!/bin/bash
    proxy_address=127.0.0.1
    proxy_port=5865
    nc -x$proxy_address:$proxy_port -X5 $*
  2. export an environment variable
    $ export GIT_PROXY_COMMAND=~/bin/git-wrapper.sh

Then you can run your usual git commands.