宿主机(host)是Ubuntu 22.04,在公司的网络中运行docker pull ubuntu
会显示网络连接超时。试过在/etc/default/docker中加代理,也试过在命令行中加代理,皆不起作用。
直到看到这个贴子才解决问题, https://forums.docker.com/t/proxy-settings-do-not-work-for-either-etc-default-docker-or-systemd-drop-ins/68891/3
root@manager:~# mkdir -p /etc/systemd/system/docker.service.d
root@manager:~# echo -e "[Service]\nEnvironment=\"HTTP_PROXY=http://proxy.example.com:80/\"" > /etc/systemd/system/docker.service.d/https-proxy.conf
root@manager:~# sudo systemctl daemon-reload
root@manager:~# sudo systemctl restart docker
root@manager:~# systemctl show --property=Environment docker
Environment=HTTPS_PROXY=http://proxy.example.com:80/
$ docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
7c3b88808835: Pull complete
Digest: sha256:8ae9bafbb64f63a50caab98fd3a5e37b3eb837a3e0780b78e5218e63193961f9
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest
做一个简单的Dockerfile
FROM ubuntu
ENV http_proxy <HTTP_PROXY>
RUN apt-get update && apt-get install -y curl
$ sudo docker build -t curl .
Sending build context to Docker daemon 4.608kB
Step 1/3 : FROM ubuntu
---> 2b4cba85892a
Step 2/3 : ENV http_proxy http://10.144.1.10:8080/
---> Running in 6ca885f013d9
Removing intermediate container 6ca885f013d9
---> 58b639cac759
Step 3/3 : RUN apt-get update && apt-get install -y curl
---> Running in b2fc4c5dab97
Get:1 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Get:2 http://archive.ubuntu.com/ubuntu focal InRelease [265 kB]
...
参考
https://www.cloudbees.com/blog/using-docker-behind-a-proxy
https://stackoverflow.com/questions/22179301/how-do-you-run-apt-get-in-a-dockerfile-behind-a-proxy