A robust and secure Docker image for apt-cacher-ng, designed for performance and best practices in a containerized environment.
This setup provides a persistent cache proxy for Debian/Ubuntu-based package installations within your Docker builds or local network, significantly speeding up build times and reducing bandwidth usage.
- Non-Root Execution: Runs as a dedicated, non-root 
acnguser for enhanced security. - Persistent Cache: Utilizes a Docker volume to ensure the package cache survives container restarts and rebuilds.
 - Correct Network Binding: Pre-configured to listen on 
0.0.0.0, resolving common503 Host not founderrors when accessed vialocalhostor127.0.0.1. - Optimized Image: Built using multi-stage best practices to keep the final image size minimal.
 
Clone the repository or save the Dockerfile and run the build command in the same directory.
docker build -t apt-cacher-ng:latest .It is crucial to mount a host directory to the container's cache volume (/var/cache/apt-cacher-ng) to persist the data.
# First, create a directory on the host to store the cache
mkdir -p "$HOME/docker-volumes/apt-cacher-ng-cache"
# Run the container
docker run -d \
  --name apt-cacher-ng \
  -p 3142:3142 \
  -v "$HOME/docker-volumes/apt-cacher-ng-cache:/var/cache/apt-cacher-ng" \
  --restart unless-stopped \
  apt-cacher-ng:latest-d: Run in detached mode.--name: Assign a memorable name.-p: Map the host's port 3142 to the container's port 3142.-v: Mount the host directory for cache persistence.--restart unless-stopped: Ensure the container automatically restarts on boot or failure.
For easier management, use the docker-compose.yml file.
docker-compose up -dTest if the service is running correctly by checking its report page.
curl -I http://localhost:3142/acng-report.htmlYou should receive a successful HTTP 200 OK status code.
HTTP/1.1 200 OK
Server: Debian Apt-Cacher NG/3.7.4
...To use the proxy, configure your Docker clients or other machines on the network.
- Open Settings > Resources > Proxies.
 - Select Manual proxy configuration.
 - Enter 
http://127.0.0.1:3142for both Web Server (HTTP) and Secure Web Server (HTTPS). - Click Apply & Restart.
 
To leverage the cache during docker build, use one of the following methods.
Method 1: Using apt.conf.d (Recommended)
This method is clean and doesn't leave environment variables set.
# Point apt to the cacher on the host
RUN echo 'Acquire::http::Proxy "http://host.docker.internal:3142";' > /etc/apt/apt.conf.d/01proxy
RUN apt-get update && apt-get install -y ...
# Clean up the proxy configuration
RUN rm /etc/apt/apt.conf.d/01proxyNote: host.docker.internal is a special DNS name that resolves to the host's IP address from within the Docker container.
Method 2: Using Environment Variables
ENV http_proxy="http://host.docker.internal:3142"
ENV https_proxy="http://host.docker.internal:3142"
RUN apt-get update && apt-get install -y ...
# Unset the variables if they are not needed for the final image
ENV http_proxy=""
ENV https_proxy=""Pass the proxy settings as environment variables.
docker run --rm -it \
  --env http_proxy="http://host.docker.internal:3142" \
  --env https_proxy="http://host.docker.internal:3142" \
  ubuntu:22.04 apt-get update