docker pull 太慢可以通过设置 docker 的国内源解决
代码语言:bash复制sudo vi /etc/docker/daemon.json写入以下内容
代码语言:json复制{
"registry-mirrors" : [
"https://mirror.ccs.tencentyun.com"
]
}重启docker服务
代码语言:bash复制systemctl restart docker.servicedocker build 太慢更改 docker 源只能解决 docker pull 时慢的问题,如果需要在构建阶段进行下载,例如 apt update 、pip install 之类的操作则需要替换对应的源。
替换 pip 的源相对简单,可以在 pip 命令时指定源,例如 pip install -i https://pypi.mirrors.ustc.edu.cn/simple requests。
替换 apt 源则比较麻烦,因为不同的 base image 可能碰到不一样的问题。例如 python-slim 镜像需要替换 /etc/apt/sources.list 文件,添加国内源后会报公钥验证的错误,见 How can I write a Dockerfile based on Debian Slim in which 'apt-get update' doesn't fail with public key errors?,根据网上教程添加公钥,又会报请先安装 gnupg:E: gnupg, gnupg2 and gnupg1 do not seem to be installed, but one of them is required for this operation,想要安装 gnupg,要先执行 apt-update 。我本来就是为了执行 apt-update,整闭环了。
使用 HTTP_PROXY/HTTPS_PROXY 环境变量设置代理,但是不支持 SOCKS 代理。
使用 proxychains docker build ...,但是它只对 docker CLI 的客户端生效,构建实际发生在 dockerd 的服务端,因此该配置无效,参考 docker国内镜像加速无效的解决办法。
因此考虑在远程(境外)服务器进行构建的方式。
方式一:
在远程服务器同步代码仓库并进行构建,然后 docker save
代码语言:sh复制# remote
docker login ccr.ccs.tencentyun.com --username=XXX
docker tag test:0.1 ccr.ccs.tencentyun.com/YOUR_NAMESPACE/test:0.1
docker push ccr.ccs.tencentyun.com/YOUR_NAMESPACE/test:0.1
# local
docker pull ccr.ccs.tencentyun.com/YOUR_NAMESPACE/test:0.1方式二:
参考 Can I build a Docker container from the CLI against a remote daemon? 和 How to Use a Remote Docker Server to Speed Up Your Workflow,通过设置远程 dockerd 服务端,本地直接请求在远程构建,免去同步代码仓库的操作。但是这样生成的镜像还是在远程,同样需要 docker save/docker load 或者 docker push/docker pull 操作同步回本地。
代码语言:sh复制ssh-copy-id root@remote_host
export DOCKER_HOST=ssh://root@remote_host
# check docker server info
docekr info | grep Name
# build with remote docker server
docker build -t test:0.1 .
# unset environment
unset DOCKER_HOST