當你有許多docker服務器,或者不能使用Docker緩存來構建不相干的Docker容器,他可以為你的包緩存代理,這是非常有用的。該容器使第二個下載的任何包幾乎瞬間下載。
使用下邊的Dockerfile
# # Build: docker build -t apt-cacher . # Run: docker run -d -p 3142:3142 --name apt-cacher-run apt-cacher # # and then you can run containers with: # docker run -t -i --rm -e http_proxy http://dockerhost:3142/ debian bash # FROM ubuntu MAINTAINER [email protected] VOLUME ["/var/cache/apt-cacher-ng"] RUN apt-get update ; apt-get install -yq 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/*
使用下邊的命令構建鏡像:
$ sudo docker build -t eg_apt_cacher_ng .
現在運行它,映射內部端口到主機
$ sudo docker run -d -p 3142:3142 --name test_apt_cacher_ng eg_apt_cacher_ng
查看日志文件,默認使用tailed
命令,你可以使用
$ sudo docker logs -f test_apt_cacher_ng
讓你Debian-based容器使用代理,你可以做三件事之一:
1.添加一個apt代理設置echo 'Acquire::http { Proxy "http://dockerhost:3142"; };' >> /etc/apt/conf.d/01proxy
2.設置環境變量:http_proxy=http://dockerhost:3142/
3.修改你的sources.list
來開始http://dockerhost:3142/
選項1注入是安全設置到你的apt配置在本地的公共基礎版本。
FROM ubuntu RUN echo 'Acquire::http { Proxy "http://dockerhost:3142"; };' >> /etc/apt/apt.conf.d/01proxy RUN apt-get update ; apt-get install vim git # docker build -t my_ubuntu .
選項2針對測試時非常好的,但是會破壞其它從http代理的HTTP客戶端,如curl 、wget或者其他:
$ sudo docker run --rm -t -i -e http_proxy=http://dockerhost:3142/ debian bash
選項3是最輕便的,但是有時候你可能需要做很多次,你也可以在你的Dockerfile
這樣做:
$ sudo docker run --rm -t -i --volumes-from test_apt_cacher_ng eg_apt_cacher_ng bash $$ /usr/lib/apt-cacher-ng/distkill.pl Scanning /var/cache/apt-cacher-ng, please wait... Found distributions: bla, taggedcount: 0 1. precise-security (36 index files) 2. wheezy (25 index files) 3. precise-updates (36 index files) 4. precise (36 index files) 5. wheezy-updates (18 index files) Found architectures: 6. amd64 (36 index files) 7. i386 (24 index files) WARNING: The removal action may wipe out whole directories containing index files. Select d to see detailed list. (Number nn: tag distribution or architecture nn; 0: exit; d: show details; r: remove tagged; q: quit): q
最後,停止測試容器,刪除容器,刪除鏡像:
$ sudo docker stop test_apt_cacher_ng $ sudo docker rm test_apt_cacher_ng $ sudo docker rmi eg_apt_cacher_ng