使用Docker部署Apt-Cacher-ng服务


https://wiki.ubuntu.org.cn/AptProxyCache
https://www.unix-ag.uni-kl.de/~bloch/acng

服务端创建Docker镜像并启动容器

创建 Dockerfile 文件,内容如下:

Dockerfile
FROM ubuntu

VOLUME ["/var/cache/apt-cacher-ng"]
RUN sed -i "s@http://.*archive.ubuntu.com@http://mirrors.tuna.tsinghua.edu.cn@g" /etc/apt/sources.list && sed -i "s@http://.*security.ubuntu.com@http://mirrors.tuna.tsinghua.edu.cn@g" /etc/apt/sources.list
RUN apt-get update ; apt-get install -yq apt-cacher-ng
RUN echo "PassThroughPattern: ^(.*):443$" >> /etc/apt-cacher-ng/acng.conf

EXPOSE 3142
CMD chmod 777 /var/cache/apt-cacher-ng ; /etc/init.d/apt-cacher-ng start ; tail -f /var/log/apt-cacher-ng/apt-cacher.log

构建镜像:

docker build -t dt_apt_cacher_ng .

创建容器并启动

docker run -d -p 3142:3142 --name dt_apt_cacher_ng dt_apt_cacher_ng

查看日志

docker logs -f dt_apt_cacher_ng

客户端配置代理

EITHER: Configure APT to use a HTTP proxy by specifying it in apt.conf or related configuration files, see apt.conf manual page for details. Server and Port need to match the values used to visit this page. For example, edit /etc/apt/apt.conf (or create a new file called like /etc/apt/apt.conf.d/00aptproxy) and add the line:
Acquire::http::Proxy "http://172.17.0.2:3142";

配置apt代理规则,http请求走代理,https请求不走代理

echo 'Acquire::http::Proxy "http://10.10.140.105:3142";' > /etc/apt/apt.conf.d/00aptproxy

# vim /etc/apt/apt.conf.d/00aptproxy
Acquire::http::Proxy "http://10.10.140.105:3142";
Acquire::https::Proxy "false";

Dockerfile中使用代理

FROM ubuntu
RUN echo 'Acquire::http::Proxy "http://10.10.140.105:3142";
Acquire::https::Proxy "false";' >> /etc/apt/apt.conf.d/00aptproxy
RUN RUN apt-get update ; apt-get install vim git

服务端支持代理https请求

https://wiki.debian.org/AptCacherNg

apt-cacher-ng显然无法为HTTPS存储库提供服务。上游有很多解决方案,但我发现最简单的解决方案是直接在代理上隧道HTTPS连接,方法是将其放入/etc/apt-cacher-ng/acng.conf:
PassThroughPattern: ^(.*):443$
据我所知,这是避免对HTTPS存储库的客户端进行特殊配置的唯一方法。缺点是HTTPS内容没有缓存。

替换国内源

https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/

sudo sed -i "s@http://.*archive.ubuntu.com@https://mirrors.tuna.tsinghua.edu.cn@g" /etc/apt/sources.list
sudo sed -i "s@http://.*security.ubuntu.com@https://mirrors.tuna.tsinghua.edu.cn@g" /etc/apt/sources.list

幻翼 2023年1月13日 16:40 收藏文档