Skip to content

xavieryin/docker-apt-cacher-ng

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

APT-Cacher NG Docker Image

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.

Features

  • Non-Root Execution: Runs as a dedicated, non-root acng user 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 common 503 Host not found errors when accessed via localhost or 127.0.0.1.
  • Optimized Image: Built using multi-stage best practices to keep the final image size minimal.

Usage

1. Build the Image

Clone the repository or save the Dockerfile and run the build command in the same directory.

docker build -t apt-cacher-ng:latest .

2. Run the Container

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.

3. Using Docker Compose (Recommended)

For easier management, use the docker-compose.yml file.

docker-compose up -d

Verification

Test if the service is running correctly by checking its report page.

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

You should receive a successful HTTP 200 OK status code.

HTTP/1.1 200 OK
Server: Debian Apt-Cacher NG/3.7.4
...

Client Configuration

To use the proxy, configure your Docker clients or other machines on the network.

Docker Desktop

  1. Open Settings > Resources > Proxies.
  2. Select Manual proxy configuration.
  3. Enter http://127.0.0.1:3142 for both Web Server (HTTP) and Secure Web Server (HTTPS).
  4. Click Apply & Restart.

Within another Dockerfile

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/01proxy

Note: 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=""

For a single docker run command

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

About

apt-cacher-ng server

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published