阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Docker 配置缓存代理服务Apt-Cacher-NG

153次阅读
没有评论

共计 3438 个字符,预计需要花费 9 分钟才能阅读完成。

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 文件,

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 这个镜像

docker build -t eg_apt_cache_ng .

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

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

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

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 容器

docker run --rm -t -i -e http_proxy=http://192.168.0.2:3142/ debian bash
 
这里也是可以使用 dockerfile 来指定
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 容器上

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

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

vim /etc/apt/apt.conf.d/02proxy # 在这个路径下创建文件

Acquire::http {Proxy "http://192.168.0.2:3142"; }; # 写入这条内容

7、测试:

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

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 上下载。

更多 Docker 相关教程见以下内容

Docker 安装应用(CentOS 6.5_x64) http://www.linuxidc.com/Linux/2014-07/104595.htm

64 位 CentOS 下安装与启动 Docker  http://www.linuxidc.com/Linux/2017-03/141714.htm

CentOS7.2 定制属于自己的 Docker 私有库  http://www.linuxidc.com/Linux/2017-03/141850.htm

Ubuntu 使用 VNC 运行基于 Docker 的桌面系统  http://www.linuxidc.com/Linux/2015-08/121170.htm

阿里云 CentOS 6.5 模板上安装 Docker http://www.linuxidc.com/Linux/2014-11/109107.htm

Ubuntu 15.04 下安装 Docker  http://www.linuxidc.com/Linux/2015-07/120444.htm

在 Ubuntu Trusty 14.04 (LTS) (64-bit)安装 Docker http://www.linuxidc.com/Linux/2014-10/108184.htm

在 Ubuntu 15.04 上如何安装 Docker 及基本用法 http://www.linuxidc.com/Linux/2015-09/122885.htm

Ubuntu 16.04 上 Docker 使用手记 http://www.linuxidc.com/Linux/2016-12/138490.htm

Docker 的详细介绍:请点这里
Docker 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-03/142416.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计3438字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中