?
Dieses Dokument verwendet PHP-Handbuch für chinesische Websites Freigeben
注意:
如果您不喜歡 sudo,請參閱授予非 root 訪問權(quán)限。
如果你通過 TCP 使用 macOS 或 docker,那么你不應(yīng)該使用 sudo。
當(dāng)你有多個 Docker 服務(wù)器,或者構(gòu)建不能使用 Docker 構(gòu)建緩存的不相關(guān)的 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 # # Here, `dockerhost` is the IP address or FQDN of a host running the Docker daemon # which acts as an APT proxy server.FROM ubuntu VOLUME ["/var/cache/apt-cacher-ng"]RUN apt-get update && apt-get install -y apt-cacher-ng EXPOSE 3142CMD chmod 777 /var/cache/apt-cacher-ng && /etc/init.d/apt-cacher-ng start && tail -f /var/log/apt-cacher-ng/*
使用以下命令構(gòu)建圖像:
$ docker build -t eg_apt_cacher_ng .
然后運(yùn)行它,將暴露的端口映射到主機(jī)上的端口
$ docker run -d -p 3142:3142 --name test_apt_cacher_ng eg_apt_cacher_ng
要查看tailed
默認(rèn)命令中的日志文件,可以使用:
$ docker logs -f test_apt_cacher_ng
要讓您的基于 Debian 的容器使用代理,您有以下選項(xiàng)。請注意,您必須使用運(yùn)行test_apt_cacher_ng
容器的主機(jī)的 IP 地址或 FQDN 進(jìn)行替換dockerhost
。
1. 添加一個 apt 代理設(shè)置 echo 'Acquire::http { Proxy "http://dockerhost:3142"; };' >> /etc/apt/conf.d/01proxy
2. 設(shè)置環(huán)境變量:http_proxy=http://dockerhost:3142/
3. 改變你的sources.list
條目從http://dockerhost:3142/
開始
4. 使用--link
將基于 Debian 的容器鏈接到 APT 代理容器
5. 使用基于 Debian 的容器創(chuàng)建 APT 代理容器的自定義網(wǎng)絡(luò)。
選項(xiàng)1將設(shè)置安全地注入本地通用基礎(chǔ)版本的 apt 配置中:
FROM ubuntu RUN echo 'Acquire::http { Proxy "http://dockerhost:3142"; };' >> /etc/apt/apt.conf.d/01proxy RUN apt-get update && apt-get install -y vim git # docker build -t my_ubuntu .
選項(xiàng)2是不錯的測試,但會破壞其他 HTTP 客戶端,其服從http_proxy
,比如curl
,wget
和其他:
$ docker run --rm -t -i -e http_proxy=http://dockerhost:3142/ debian bash
選項(xiàng)3是最不便攜的,但有時您可能需要這樣做,并且您也可以從Dockerfile
中選擇。
選項(xiàng)4使用以下命令將 Debian 容器鏈接到代理服務(wù)器:
$ docker run -i -t --link test_apt_cacher_ng:apt_proxy -e http_proxy=http://apt_proxy:3142/ debian bash
選項(xiàng)5創(chuàng)建了一個 APT 代理服務(wù)器和基于 Debian 的容器的自定義網(wǎng)絡(luò):
$ docker network create mynetwork $ docker run -d -p 3142:3142 --network=mynetwork --name test_apt_cacher_ng eg_apt_cacher_ng $ docker run --rm -it --network=mynetwork -e http_proxy=http://test_apt_cacher_ng:3142/ debian bash
Apt-cacher-ng 有一些工具可以讓您管理存儲庫,并且可以通過利用VOLUME
指令和我們構(gòu)建的用于運(yùn)行服務(wù)的映像來使用它們:
$ docker run --rm -t -i --volumes-from test_apt_cacher_ng eg_apt_cacher_ng bash root@f38c87f2a42d:/# /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
最后,通過停止并取出容器,然后移除圖像,在測試后清理。
$ docker stop test_apt_cacher_ng $ docker rm test_apt_cacher_ng $ docker rmi eg_apt_cacher_ng