Docker 代理分為兩種,一種是為運(yùn)行的 Container 配置代理,用于下載一些依賴包" />

国产成人精品无码青草_亚洲国产美女精品久久久久∴_欧美人与鲁交大毛片免费_国产果冻豆传媒麻婆精东

18143453325 在線咨詢 在線咨詢
18143453325 在線咨詢
所在位置: 首頁(yè) > 營(yíng)銷資訊 > 建站知識(shí) > Docker 代理脫坑指南

Docker 代理脫坑指南

時(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 代理分為兩種,一種是為運(yùn)行的 Container 配置代理,用于下載一些依賴包以及訪問(wèn)公網(wǎng)。另一種是為 Docker Daemon 配置代理,用于支持 docker 相關(guān)的命令。

為容器配置代理

配置容器代理一般分為兩種,一種是全局配置,另一種是僅為某個(gè)容器配置。

全局配置

首先說(shuō)明,此修改方法僅支持 17.07 或者更高版本。

修改或創(chuà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

此時(shí)宿主機(jī)并沒配置代理,查詢下 IP:

[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)成功配置了代理,可以正常下載依賴了。

局部修改

方法1-在 docker run 命令添加參數(shù)

# 創(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 添加

這里以打包一個(gè) httpd 的 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, 需要再做一步額外的處理。

方法1-通過(guò) docker run 參數(shù)添加

# 創(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 配置添加

在每個(gè) container 運(yùn)行前,會(huì)繼承 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"]}效果一樣這里就不演示了。

為 Docker Daemon 添加代理

和 container 的情況一樣,如果不為 Docker Daemon 配置代理的話,是無(wú)法使用 search, pull, push 等命令的。

配置如下:

# 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/

參考

docker-container-proxy

docker-dns

docker-daemon-proxy

關(guān)鍵詞:指南,代理

74
73
25
news

版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。

為了最佳展示效果,本站不支持IE9及以下版本的瀏覽器,建議您使用谷歌Chrome瀏覽器。 點(diǎn)擊下載Chrome瀏覽器
關(guān)閉