Skip to content

gnzsnz/apt-cacher-ng

Repository files navigation

APT-Cacher-ng docker image

A docker image to run a local apt-cacher-ng for your containers and servers.

The container will run cron daily to clean-up the cache.

Build the Image

To build the image you will need to edit the .env-dist file with your prefered setup

cp .env-dist .env
nano .env

Set UID and GID as per your needs. You can optionally define an apt proxy that will be used to build the docker image.

UID=1000
GID=1000
APT_PROXY=http://apt_proxy:3142

Please note that the APT_PROXY defined in the .env file will be used to speed up the image build only.

Run docker compose config and check that everything looks good. To build the image using docker compose you can do

docker compose build

Or with docker build

docker build --build-arg UID="$(id -u)" \
            --build-arg GID="$(id -g)" \
            --build-arg APT_PROXY="http://apt_proxy:3142" \
            -t gnzsnz/apt-cacher-ng:latest .

UID and GID are used to map the host user to the apt-cacher-ng user in the container. The image volumes will use this UID and GID.

Run apt-cacher-ng

Simplest way would be docker compose up, you might modify the docker-compose.yml file provided to adjust it to your needs.

Or alternatively with

docker run -it gnzsnz/apt-cacher-ng:latest aptcacher

Your apt-cacher-ng should be available at [http://hostname:3142/]

Setup clients

To use the apt cache proxy you need to setup your clients. This can be done by running the following line on each client.

echo 'Acquire::http { Proxy "http://proxy:3142"; }' | sudo tee  /etc/apt/apt.conf.d/02proxy
sudo apt update

To use apt-cacher in your containers you need to define in your Dockerfile

ARG APT_PROXY
RUN echo 'Acquire::http { Proxy "'$APT_PROXY'"; }'  \
    | tee /etc/apt/apt.conf.d/02proxy &&\
    apt-get update && apt-get -y install ...

You will need to pass the apt-cache-ng address ARG to build the image,

docker build \
  --build-arg APT_PROXY="http://apt-cacher:3142" -t your/image .

I cover the steps in this blog entry.

Clean up

To clean up everything

docker compose down --rmi all -v