Speed Up Docker Builds with apt-cacher-ng

A tiny thing you can do to save a ton of time

Add an apt cache in Docker so that containers don't have to re-download packages needlessly on every build. This can significantly reduce build times and conserve bandwidth by caching frequently used packages locally.

Step 1: Set Up apt-cacher-ng in Docker

First, pull and run the apt-cacher-ng container:

docker pull sameersbn/apt-cacher-ng docker run -d --name apt-cacher-ng -p 3142:3142 sameersbn/apt-cacher-ng 

This command will start the apt-cacher-ng server on your host machine, listening on port 3142.

Step 2: Configure Your Dockerfile

To use the apt-cacher-ng proxy in your Docker builds, add the following line to your Dockerfile:

RUN echo 'Acquire::http::Proxy "http://host.docker.internal:3142";' > /etc/apt/apt.conf.d/01proxy

This line configures the apt package manager to use the caching proxy running on your host machine, reducing redundant downloads.

Step 3: Monitor Cache Usage

You can access cache statistics and configuration at the following URL:

http://localhost:3142/acng-report.html

This page provides detailed information about cache hits, misses, and other useful metrics to help you optimize your caching setup.

Written by Mark on Sunday August 4, 2024
Permalink -

« Sub centimeter accuracy from your GPS -