在這某些場合中有的公司或環境的網路都是有隔離的,使用上其實不太方便,但由於這塊不是自己管也不想給他搶來管,所有的上網都要透過 Proxy 代理才能用,有的還要翻牆才能用,往往麻煩的都不是技術的實作,反而是週遭的事才是障礙,所以很多日常更新套件如 Docker Image的部份就要特別設置,尤其是在中國境內有的還要指向本地的鏡像加速,不然速度真的會慢到哭出來,下面就記錄一下這幾天遇到的幾個 Proxy 的解決方法.
#1.yum / apt-get
YUM 主要是吃 /etc/yum.conf
在 yum.conf 中加入下列的幾行
proxy=http://proxy.server:port
proxy_username=username
proxy_password=password
$ export http_proxy=http://user:password@proxy.server:port $ export https_proxy=https://user:password@proxy.server:port
PS: 自己有遇過有的應用程式上網代理的環境變量會有抓不到的情況,如果不行就再加一組大寫的變數 HTTP_PROXY 及 HTTPS_PROXY
APT 主要是吃 /etc/apt/apt.conf.d/proxy.conf
HTTP
Acquire::http::Proxy "http://user:password@proxy.server:port/";
HTTPS
Acquire::https::Proxy "http://user:password@proxy.server:port/";
#2.Wget
只要有加前面的 yum 的環境變數,應該就抓的到,若是不行,就在 ~/.wgetrc 加入下面三行
http_proxy = http://user:password@proxy.server:port/ http_proxy = http://user:password@proxy.server:port/ use_proxy = on
也可以在 wget 指令加入 -e 去指定
wget -e "http_proxy=http://user:password@proxy.server:port" www.example.com or wget -e "https_proxy=http://user:password@proxy.server:port" www.example.com
#3.Curl
有加前面的 yum 的環境變數,應該就抓的到,也可以在 curl 指令加入 -x 去指定
curl -x "http://user:password@proxy.server:port" www.example.com
#4.docker
編輯 ~/.docker/config.json 或 /etc/docker/daemon.json (按你需求可以加入 httpProxy , httpsProxy 或 ftpProxy)
{
"proxies":
{
"default":
{
"httpProxy": "http://127.0.0.1:3001",
"httpsProxy": "http://127.0.0.1:3001",
"noProxy": "*.test.example.com,.example2.com"
}
}
}
也可以在指令中或環境變數中加入下面變數
Variable | Dockerfile example | docker run Example |
---|---|---|
HTTP_PROXY |
ENV HTTP_PROXY "http://127.0.0.1:3001" |
--env HTTP_PROXY="http://127.0.0.1:3001" |
HTTPS_PROXY |
ENV HTTPS_PROXY "https://127.0.0.1:3001" |
--env HTTPS_PROXY="https://127.0.0.1:3001" |
FTP_PROXY |
ENV FTP_PROXY "ftp://127.0.0.1:3001" |
--env FTP_PROXY="ftp://127.0.0.1:3001" |
NO_PROXY |
ENV NO_PROXY "*.test.example.com,.example2.com" |
--env NO_PROXY="*.test.example.com,.example2.com" |
Reference Link: https://docs.docker.com/network/proxy/
#5. docker-compose
[Service]
# NO_PROXY is optional and can be removed if not needed
# Change proxy_url to your proxy IP or FQDN and proxy_port to your proxy port
Environment="HTTP_PROXY=http://proxy_url:proxy_port" "NO_PROXY=localhost,127.0.0.0/8"
sudo systemctl daemon-reload
sudo systemctl show docker --property Environment
sudo systemctl restart docker
Reference Link: https://stackoverflow.com/questions/54218632/how-to-use-local-proxy-settings-in-docker-compose
#6.docker build
你必須 登入 才能發表評論。