linux系统中的docker经由路由器中的Clash科学上网
在linux中使用:
启用路由器的代理 , 通常是
http://192.168.9.1:7890密码(代理认证信息)一般可在界面中获取,如
Clash:nSik86ew将上述组合起来就可以在linux中访问
curl -v -x http://Clash:nSik86ew@192.168.9.1:7890 https://www.google.com
# 若是没有密码直接就是
curl -v -x http://192.168.9.1:7890 https://www.google.com
curl -v -x http://192.168.9.124:7890 https://www.google.com临时设置带认证的环境变量
在你的 Linux 终端里,使用如下命令(注意用户名和密码大小写):
export http_proxy="http://Clash:nSik86ew@192.168.9.1:7890"
export https_proxy="http://Clash:nSik86ew@192.168.9.1:7890"
export all_proxy="http://Clash:nSik86ew@192.168.9.1:7890"
export http_proxy="http://192.168.9.124:7890"
export https_proxy="http://192.168.9.124:7890"
export all_proxy="http://192.168.9.124:7890"
这样,终端里大部分支持代理的命令(如 curl、wget、pip、apt 等)都会走代理,并自动携带认证信息。
# 临时取消代理环境变量
unset http_proxy
unset https_proxy
unset all_proxy永久设置
将上面三行加到你的 ~/.bashrc 或 ~/.zshrc 文件末尾,然后执行 source ~/.bashrc 或重启终端即可。
验证
# 直接访问
curl -v https://www.google.com
# 查询外网Ip
curl ifconfig.me
curl ipinfo.io/ip
systemctl show --property=Environment docker在linux中的Docker中使用:
Docker daemon 默认不会读取 shell 的 http_proxy 环境变量。
需要专门配置 Docker 的代理。
直接用 shell 的 http_proxy/https_proxy 环境变量对 docker pull 是无效的**,因为:
docker pull实际上是由 Docker daemon(后台服务) 发起的网络请求,而不是你的 shell 进程。- Docker daemon 不会自动继承你 shell 里的环境变量。
正确让 docker pull 走 HTTP 代理的方法
1. 配置 Docker daemon 的代理(推荐)
假设你的代理信息如下:
- 代理服务器:192.168.9.1
- 端口:7890
- 用户名:Clash
- 密码:nSik86ew
步骤如下:
- 新建配置目录
sudo mkdir -p /etc/systemd/system/docker.service.d- 新建代理配置文件
sudo bash -c 'cat > /etc/systemd/system/docker.service.d/http-proxy.conf <<EOF
[Service]
Environment="HTTP_PROXY=http://Clash:nSik86ew@192.168.9.1:7890/"
Environment="HTTPS_PROXY=http://Clash:nSik86ew@192.168.9.1:7890/"
EOF'
sudo bash -c 'cat > /etc/systemd/system/docker.service.d/http-proxy.conf <<EOF
[Service]
Environment="HTTP_PROXY=http://192.168.9.124:7890/"
Environment="HTTPS_PROXY=http://192.168.9.124:7890/"
EOF'- 重载 systemd 并重启 Docker
sudo systemctl daemon-reload
sudo systemctl restart docker- 验证代理配置
docker info | grep -i proxy应该能看到你刚刚设置的代理地址。
- 重新拉取镜像
docker pull questdb/questdb:nightly-rhel其他说明
- 如果代理用户名或密码有特殊字符(如
@、:),需要进行URL编码。 - 如果你在用 root 用户,操作方法一样。
- 如果你有多个 Docker 配置文件(比如
/etc/docker/daemon.json),也可以在里面设置"proxies"字段,但推荐 systemd 方式,兼容性更好。
总结
docker pull 不能直接用 shell 的 http_proxy 环境变量,必须为 Docker daemon 配置代理。