且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

docker 配置缓存代理服务apt-cacher-ng

更新时间:2022-10-13 10:12:01

Apt-Cacher-NG是一个缓存代理服务(或者apt代理),对基于Debian的设备,如 Ubuntu, Kubuntu, Xubuntu, Edubuntu, Linux Mint等,它被是用来缓存已经下载过的包。

docker 配置缓存代理服务apt-cacher-ng

你有几台电脑链接的网络,你想手动在每台机器上安装或者更新软件包,(可想而知)这是一个相当困难的任务而且耗费时间;这就是我们为什么要配置一个apt-cacher-ng服务到所有系统这个伟大想法的原因,因为它将从网络首次缓存所有已下载包到 apt-cache server,剩余的Debian, Ubuntu机器获得这些软件包就只需从Apt-Cache直接获取,这将节约我们的宝贵的时间和网络带宽。

特点

  1. apt-cacher-ng 将节约我们的时间.

  2. apt-cacher-ng 将节约我们的带宽.

  3. 通过导入参数,我们可以整合 ISO image data 或者 DVD到 apt-cacher-ng。

本次实验使用docker容器来搭建:

1、首先创建Dockerfile文件,

1
2
3
4
5
6
7
8
root@ubuntu:~/docker/apt-cache-ng# cat Dockerfile 
FROM        ubuntu
 
VOLUME      ["/var/cache/apt-cacher-ng"]
RUN     apt-get update && apt-get install -y apt-cacher-ng
 
EXPOSE      3142
CMD     chmod 777 /var/cache/apt-cacher-ng && /etc/init.d/apt-cacher-ng start && tail -f /var/log/apt-cacher-ng/*

2、build这个镜像

1
docker build -t eg_apt_cache_ng .

3、启动镜像,并且把端口映射到宿主机的13142上

1
docker run -d -p 13142:3142 --name test_apt_cacher_ng eg_apt_cacher_ng

4、可以使用docker命令来查看日志

1
docker logs -f test_apt_cacher_ng

5、登录浏览器查看;

docker 配置缓存代理服务apt-cacher-ng

docker 配置缓存代理服务apt-cacher-ng

这里,我们可以看到 apt-cacher-ng的报告页面,点击静态报告,配置页面的底部,导航到下载命中或者失误的情况页面。

在报告页面我们需要复制Proxy URL以便后边使用。我们可以安装包在这个server上,通过配置本地参数,添加实体/etc/apt/apt.conf.d/02proxy的 apt-cache。

Acquire::http { Proxy "http://192.168.0.2:3142"; };

docker官网的说明:

To get your Debian-based containers to use the proxy, you have following options. Note that you must replace dockerhost with the IP address or FQDN of the host running the test_apt_cacher_ng container.

  1. Add an apt Proxy setting echo 'Acquire::http { Proxy "http://dockerhost:3142"; };' >> /etc/apt/conf.d/01proxy

  2. Set an environment variable: http_proxy=http://dockerhost:3142/

  3. Change your sources.list entries to start with http://dockerhost:3142/

  4. Link Debian-based containers to the APT proxy container using --link

  5. Create a custom network of an APT proxy container with Debian-based containers.


6、创建客户端的docker 容器

1
docker run --rm -t -i -e http_proxy=http://192.168.0.2:3142/ debian bash

这里也是可以使用dockerfile来指定

1
2
3
FROM ubuntu
RUN  echo 'Acquire::http { Proxy "http://192.168.0.2:3142"; };' >> /etc/apt/apt.conf.d/01proxy
RUN apt-get update && apt-get install -y vim git

链接容器到apt容器上

1
 docker run -i -t --link test_apt_cacher_ng:apt_proxy -e http_proxy=http://apt_proxy:3142/ debian bash

你也可以自己创建一个容器,然后进入到容器中,去手动配置

1
2
 vim /etc/apt/apt.conf.d/02proxy #在这个路径下创建文件
 Acquire::http { Proxy "http://192.168.0.2:3142"; }; #写入这条内容

7、测试:

到处我们服务端和客户端都安装完毕

1
2
3
4
root@ubuntu:~/docker/apt-cache-ng# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                     NAMES
bd4e542edb39        debian              "bash"                   59 minutes ago      Up 59 minutes                                 peaceful_pike
f6fafb226c66        eg_apt_cache_ng     "/bin/sh -c 'chmod..."   About an hour ago   Up About an hour    0.0.0.0:13142->3142/tcp   test_apt_cacher_ng

登录到客户端中,去更新几个包,或者安装几个包,然后再web上面可以看到缓存来多少个文件

docker 配置缓存代理服务apt-cacher-ng

这里你可以把安装包,卸载,然后再次安装,你会发现速度会非常快,而且web上面也会hits也会有变动来显示你是否从apt上下载




本文转自 kesungang 51CTO博客,原文链接:http://blog.51cto.com/sgk2011/1911515,如需转载请自行联系原作者