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.
apt-cacher-ng
in DockerFirst, 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
.
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.
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.