時(shí)間:2023-02-10 21:48:01 | 來(lái)源:建站知識(shí)
時(shí)間:2023-02-10 21:48:01 來(lái)源:建站知識(shí)
由于公司 Lab 服務(wù)器無(wú)法正常訪問(wèn)公網(wǎng),想要下載一些外部依賴包需要配置公司的內(nèi)部代理。Docker 也是同理,想要訪問(wèn)公網(wǎng)需要配置一定的代理。~/.docker/config.json
# 如果有的話,先備份一下cp ~/.docker/config.json ~/.docker/config.json.bk# 修改內(nèi)容如下cat ~/.docker/config.json{ "auths": {}, "HttpHeaders": { "User-Agent": "Docker-Client/19.03.2 (linux)" }, "proxies": { "default": { "httpProxy": "http://173.39.112.117:80", "httpsProxy": "http://173.39.112.117:80" } }}
為了確保生效,重啟下 docker :systemctl restart docker
[root@localhost ~]# curl cip.ccIP : 64.104.xxx.xx地址 : 中國(guó) 香港 cisco.com數(shù)據(jù)二 : 香港 | 特別行政區(qū)數(shù)據(jù)三 : 中國(guó)香港URL : http://www.cip.cc/64.104.xxx.xx
基于之前使用 Docker file 打包鏡像的文章,直接使用打包好帶有 systemd 功能的鏡像。# 創(chuàng)建 container[root@localhost home] docker run --privileged=true -ti /-v /sys/fs/cgroup:/sys/fs/cgroup:ro /-p 80:80 -d local/c7-systemd# 進(jìn)入 container[root@localhost home] docker exec -it 3eaa1cc71706 /bin/bash# 查詢 IP[root@3eaa1cc71706 /]# curl cip.ccIP : 173.39.112.xxx地址 : 新加坡 新加坡數(shù)據(jù)二 : 新加坡數(shù)據(jù)三 : 新加坡URL : http://www.cip.cc/173.39.112.xxx
可以看到容器內(nèi)已經(jīng)成功配置了代理,可以正常下載依賴了。# 創(chuàng)建 containerdocker run --privileged=true -ti /-v /sys/fs/cgroup:/sys/fs/cgroup:ro /--env HTTP_PROXY="http://173.39.112.117:80 /--env HTTPS_PROXY="http://173.39.112.117:80 /--env http_proxy="http://173.39.112.117:80 /--env https_proxy="http://173.39.112.117:80 /-p 80:80 -d local/c7-systemd# 進(jìn)入 container[root@localhost home]# docker exec -it 3607976e8f2d /bin/bash# 查詢 IP[root@3607976e8f2d /]# curl cip.ccIP : 173.39.112.xxx地址 : 新加坡 新加坡數(shù)據(jù)二 : 新加坡數(shù)據(jù)三 : 新加坡URL : http://www.cip.cc/173.39.112.xxx
方法2-在 Docker-file 添加FROM local/c7-systemdENV MY_PROXY_URL="http://173.39.112.117:80"ENV HTTP_PROXY=$MY_PROXY_URL / HTTPS_PROXY=$MY_PROXY_URL / FTP_PROXY=$MY_PROXY_URL / http_proxy=$MY_PROXY_URL / https_proxy=$MY_PROXY_URL / ftp_proxy=$MY_PROXY_URLRUN yum -y install httpd; yum clean all; systemctl enable httpd.serviceEXPOSE 80CMD ["/usr/sbin/init"]
結(jié)果是相同的,這里就不演示了。有時(shí)添加代理是域名的話,就需要額外的操作。proxy.esl.cisco.com:80
, 需要再做一步額外的處理。# 創(chuàng)建 container[root@localhost home]#docker run --privileged=true -ti /-v /sys/fs/cgroup:/sys/fs/cgroup:ro /--env HTTP_PROXY="http://proxy.esl.cisco.com:80 /--env HTTPS_PROXY="http://proxy.esl.cisco.com:80 /--env http_proxy="http://proxy.esl.cisco.com:80 /--env https_proxy="http://proxy.esl.cisco.com:80 /--dns=64.104.123.245 /-p 80:80 -d local/c7-systemd# 進(jìn)入 container[root@localhost home]# docker exec -it 992dc27de1cc /bin/bash# 查看 IP[root@992dc27de1cc /]# curl cip.ccIP : 173.39.xxx.xxx地址 : 新加坡 新加坡數(shù)據(jù)二 : 新加坡數(shù)據(jù)三 : 新加坡URL : http://www.cip.cc/173.39.xxx.xxx
方法2-通過(guò)修改 docker daemon 配置添加/etc/docker/daemon.json
文件下.# 為 docker daemon 添加 dns,在運(yùn)行時(shí)會(huì)為每個(gè) container 添加上cat /etc/docker/daemon.json{ "dns" : [ "8.8.4.4", "8.8.8.8", "Your_DNS_SERVER" ], "registry-mirrors":["https://docker.mirrors.ustc.edu.cn"]}
效果一樣這里就不演示了。# STEP1-創(chuàng)建文件夾[root@localhost home]# sudo mkdir -p /etc/systemd/system/docker.service.d# STEP2-創(chuàng)建代理文件 http 和 https[root@localhost home]# cat /etc/systemd/system/docker.service.d/http-proxy.conf[Service]Environment="HTTP_PROXY=http://proxy.esl.cisco.com:80/"[root@localhost home]# cat /etc/systemd/system/docker.service.d/https-proxy.conf[Service]Environment="HTTPS_PROXY=http://proxy.esl.cisco.com:80/"# 如果希望訪問(wèn)某些 Docker registries 不是用代理,可以在上面的配置文件后追加[Service] Environment="HTTP_PROXY=http://proxy.example.com:80/" "NO_PROXY=localhost,127.0.0.1,docker-registry.example.com,.corp"# STEP3-刷新變更[root@localhost home]# sudo systemctl daemon-reload# STEP4-重啟 Docker[root@localhost home]# sudo systemctl restart docker# STEP5-驗(yàn)證代理是否生效[root@localhost home]# systemctl show --property=Environment dockerEnvironment=HTTP_PROXY=http://proxy.esl.cisco.com/ HTTPS_PROXY=http://proxy.esl.cisco.com:80/
關(guān)鍵詞:指南,代理
客戶&案例
營(yíng)銷資訊
關(guān)于我們
客戶&案例
營(yíng)銷資訊
關(guān)于我們
微信公眾號(hào)
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。