docker 配置緩存代理服務apt-cacher-ng

Apt-Cacher-NG是一個緩存代理服務(或者apt代理),對基於Debian的設備,如 Ubuntu, Kubuntu, Xubuntu, Edubuntu, Linux Mint等,它被是用來緩存已經下載過的包。
html

wKioL1jbgyzxjb9AAAFjNQixMhc202.png

你有幾臺電腦連接的網絡,你想手動在每臺機器上安裝或者更新軟件包,(可想而知)這是一個至關困難的任務並且耗費時間;這就是咱們爲何要配置一個apt-cacher-ng服務到全部系統這個偉大想法的緣由,由於它將從網絡首次緩存全部已下載包到 apt-cache server,剩餘的Debian, Ubuntu機器得到這些軟件包就只需從Apt-Cache直接獲取,這將節約咱們的寶貴的時間和網絡帶寬。git

特色web

  1. apt-cacher-ng 將節約咱們的時間.docker

  2. apt-cacher-ng 將節約咱們的帶寬.ubuntu

  3. 經過導入參數,咱們能夠整合 ISO p_w_picpath data 或者 DVD到 apt-cacher-ng。vim

本次實驗使用docker容器來搭建:瀏覽器

一、首先建立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/*

二、build這個鏡像
bash

docker build -t eg_apt_cache_ng .

三、啓動鏡像,而且把端口映射到宿主機的13142上網絡

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

四、可使用docker命令來查看日誌

docker logs -f test_apt_cacher_ng

五、登陸瀏覽器查看;

wKioL1jbhV6htjA_AAEZZsPnZ_8744.png-wh_50

wKiom1jbhZLBIRp4AAB6E_3pxtY989.png

這裏,咱們能夠看到 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.


六、建立客戶端的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"; }; #寫入這條內容

七、測試:

處處咱們服務端和客戶端都安裝完畢

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上面能夠看到緩存來多少個文件

wKiom1jbiEHj0jmLAAB_TOIoztc228.png-wh_50

這裏你能夠把安裝包,卸載,而後再次安裝,你會發現速度會很是快,並且web上面也會hits也會有變更來顯示你是否從apt上下載



官網:https://www.unix-ag.uni-kl.de/~bloch/acng/html/index.html

docker官網:https://docs.docker.com/engine/examples/apt-cacher-ng/

參考博文:http://liuhong1happy.lofter.com/post/1cdb27c8_60dfc24

相關文章
相關標籤/搜索