Speed up your docker builds using apt-cacher-ng

Speed up your docker builds using apt-cacher-ng

Install apt-cacher-ng on the host machine

sudo apt install apt-cacher-ng

Check if your apt-cacher-ng service is running by visiting localhost:3142 in your web browser

Check the ip your local docker0 interface

> ip a
5: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:60:c7:b6:6b brd ff:ff:ff:ff:ff:ff
    inet brd scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:60ff:fec7:b66b/64 scope link 
       valid_lft forever preferred_lft forever

Provide the apt-cacher-ng service URL to your apt-based Dockerfile builds

In your Dockerfile builds use the proxy url for apt-cacher-ng by inserting this additional line

RUN echo 'Acquire::http { Proxy ""; };' >> /etc/apt/apt.conf.d/01proxy

This will work with any operating system base images whose repository system is supported by apt-cacher-ng. More details on apt-cacher-ng website