公司内网的特点是没有直连互联网,需要代理服务器,下面是如何给docker设置代理服务器。
安装完docker后,直接运行docker pull
会遇到如下连接错误。
$ docker pull hello-world
Using default tag: latest
Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
这是因为直接连接docker.io
是不通的,用curl
命令也可以验证。
$ curl docker.io -v
* Trying 3.225.11.9:80...
* TCP_NODELAY set
等很长时间也不会有结果
但是通过设置代理就不一样了。
$ export http_proxy=http://10.144.1.10:8080
$ curl docker.io -v
* Uses proxy env variable http_proxy == 'http://10.144.1.10:8080'
* Trying 10.144.1.10:8080...
* TCP_NODELAY set
* Connected to 10.144.1.10 (10.144.1.10) port 8080 (#0)
> GET http://docker.io/ HTTP/1.1
> Host: docker.io
> User-Agent: curl/7.68.0
> Accept: */*
> Proxy-Connection: Keep-Alive
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 301 Moved Permanently
< Date: Wed, 02 Jun 2021 02:47:04 GMT
< content-length: 0
< location: https://docker.io/
< Via: 1.1 fihel1e-proxy.emea.nsn-net.net
<
* Connection #0 to host 10.144.1.10 left intact
其中10.144.1.10:8080
为公司代理,通过上述export+curl
命令可以知道代理服务器是正常工作的。
因为docker
是后台运行的服务,所以环境变量的设置要放在docker.service
中。
$ sudo vi /lib/systemd/system/docker.service
在[Service]
中增加:
[Service]
Environment="HTTPS_PROXY=http://10.144.1.10:8080/"
保存文件。
重启docker.service
sudo systemctl daemon-reload
sudo systemctl restart docker.service
下载hello-world
louyang@wk:~/foo/docker$ docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete
Digest: sha256:5122f6204b6a3596e048758cabba3c46b1c937a46b5be6225b835d091b90e46c
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest
louyang@wk:~/foo/docker$
louyang@wk:~/foo/docker$ docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 2 months ago 13.3kB
如果遇到如下错误
$ docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
toomanyrequests: You have reached your pull rate limit. You may increase the limit by authenticating and upgrading: https://www.docker.com/increase-rate-limit
需要登录
$ docker login
用户名和密码在https://hub.docker.com/
免费注册。
如果遇到加代理后公司内网使用问题,可以在docker.service
中加入:
[Service]
Environment="NO_PROXY="localhost,127.0.0.1,::1,<公司名称>.com"
参考文献
https://www.serverlab.ca/tutorials/containers/docker/how-to-set-the-proxy-for-docker-on-ubuntu/