背景
在一些实验室环境,服务器没有访问外网的权限,需要通过http代理。我们通常会将网络代理直接配置/etc/profile的配置文件中,这对于大部分操作都是可行的。然而,docker命令却使用不了这些代理。比如docker pull时需要从外网下载镜像,就会出现如下错误:
$ sudo docker pull hello-world Unable to find image 'hello-world:latest' locally Pulling repository docker.io/library/hello-world docker: Network timed out while trying to connect to https://index.docker.io/v1/repositories/library/hello-world/images. You may want to check your internet connection or if you are behind a proxy.. See 'docker run --help'.
解决方案
# 创建 /etc/systemd/system/docker.service.d $ sudo mkdir -p /etc/systemd/system/docker.service.d # $ sudo vim /etc/systemd/system/docker.service.d/http_proxy.conf [Service] Environment="HTTP_PROXY=http://${proxy-addr}:${proxy-port}/" "HTTPS_PROXY=https://${proxy-addr}:${proxy-port}/" "NO_PROXY=localhost,127.0.0.1,docker-registry.somecorporation.com" # 更新配置并重启 docker 服务 $ sudo systemctl daemon-reload && sudo systemctl restart docker.service