Dockerize an apt-cacher-ng service
预计阅读时间:4分钟
Note:
- 如果您不喜欢 sudo ,请参阅Giving non-root access .
- 如果你通过 TCP 使用 macOS 或 docker,那么你不应该使用 sudo.
当您有多个 Docker 服务器,或构建无法使用 Docker 构建缓存的不相关 Docker 容器时,为您的包提供缓存代理会很有用. 这个容器使任何包的第二次下载几乎是即时的.
使用以下 Dockerfile:
# syntax=docker/dockerfile:1
# 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 3142
CMD chmod 777 /var/cache/apt-cacher-ng && /etc/init.d/apt-cacher-ng start && tail -f /var/log/apt-cacher-ng/*
要使用以下方法构建图像:
$ docker build -t eg_apt_cacher_ng .
然后运行它,将暴露的端口映射到主机上的一个
$ docker run -d -p 3142:3142 --name test_apt_cacher_ng eg_apt_cacher_ng
要查看默认tailed
中的日志文件,您可以使用:
$ docker logs -f test_apt_cacher_ng
要让基于 Debian 的容器使用代理,您有以下选项. 将dockerhost
替换为运行test_apt_cacher_ng
容器的主机的 IP 地址或 FQDN.
- 添加一个 apt 代理设置
echo 'Acquire::http { Proxy "http://dockerhost:3142"; };' >> /etc/apt/apt.conf.d/01proxy
echo 'Acquire::http { Proxy "http://dockerhost:3142"; };' >> /etc/apt/apt.conf.d/01proxy
- 设置环境变量:
http_proxy=http://dockerhost:3142/
- 将您的
sources.list
条目更改为以http://dockerhost:3142/
- 使用
--link
将基于 Debian 的容器链接到 APT 代理容器 - 使用基于 Debian 的容器创建 APT 代理容器的自定义网络.
选项 1将设置安全地注入到本地版本的公共基础中的 apt 配置中:
# syntax=docker/dockerfile:1
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 .
选项 2有利于测试,但会破坏其他遵循http_proxy
的 HTTP 客户端,例如curl
、 wget
等:
$ docker run --rm -t -i -e http_proxy=http://dockerhost:3142/ debian bash
选项 3是最不便携的,但您可能需要这样做,您也可以从Dockerfile
中进行.
选项 4使用以下命令将 Debian 容器链接到代理服务器:
$ docker run -i -t --link test_apt_cacher_ng:apt_proxy -e http_proxy=http://apt_proxy:3142/ debian bash
选项 5创建 APT 代理服务器和基于 Debian 的容器的自定义网络:
$ 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
指令和我们为运行服务而构建的映像来使用:
$ 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 container stop test_apt_cacher_ng
$ docker container rm test_apt_cacher_ng
$ docker image rm eg_apt_cacher_ng