设置docker代理 (Linux版)
- docker拉取代理(守护进程代理)
sudo mkdir -p /etc/systemd/system/docker.service.d
sudo vim /etc/systemd/system/docker.service.d/http-proxy.conf
[Service]
Environment="HTTP_PROXY=http://192.168.137.1:10809"
Environment="HTTPS_PROXY=http://192.168.137.1:10809"
- 容器代理
~/.docker/config.json
{
"proxies":
{
"default":
{
"httpProxy": "http://192.168.137.1:10809",
"httpsProxy": "http://192.168.137.1:10809",
"noProxy": "localhost,127.0.0.1,.example.com"
}
}
}
前两个代理配置后都需要重启docker和守护进程以生效
sudo systemctl daemon-reload
sudo systemctl restart docker
- 容器内代理
build已经废弃,现在都用buildx,一般按照官方的一键安装指令安装,都自带(curl -fsSL https://get.docker.com | sudo bash)。如果不自带,就手动安装,可以问ai
docker build . \
--build-arg "HTTP_PROXY=http://192.168.137.1:10809/" \
--build-arg "HTTPS_PROXY=http://192.168.137.1:10809/" \
--build-arg "NO_PROXY=localhost,127.0.0.1,.example.com" \
-t
docker build \
--build-arg HTTP_PROXY=http://host.docker.internal:10809 \
--build-arg HTTPS_PROXY=http://host.docker.internal:10809 \
-t openmw .
docker build \
--build-arg HTTP_PROXY=http://192.168.137.1:10809 \
--build-arg HTTPS_PROXY=http://192.168.137.1:10809 \
-t openmw .
Dockerfile里使用
ARG HTTP_PROXY
ARG HTTPS_PROXY
#就可以获取
ENV http_proxy=$HTTP_PROXY
ENV https_proxy=$HTTPS_PROXY
#不想每次docker build传参那就直接写死
ENV http_proxy=http://192.168.137.1:10809
ENV https_proxy=http://192.168.137.1:10809
# 不确定的话,用这个也可以
ENV HTTP_PROXY=http://host.docker.internal:10809
ENV HTTPS_PROXY=http://host.docker.internal:10809
# 容器若是fedora(实际上属于RedHat类)则给dnf设置代理(类似debian系的apt)
RUN echo "proxy=http://host.docker.internal:10809" >> /etc/dnf/dnf.conf
Docker Desktop 设置代理(巨坑)
- cmd命令行执行
ipconfig,可以看到wsl网卡的ipv4地址
以太网适配器 vEthernet (WSL (Hyper-V firewall)):
连接特定的 DNS 后缀 . . . . . . . :
本地链接 IPv6 地址. . . . . . . . : fe80::10a4:4cba:ff25:4095%59
IPv4 地址 . . . . . . . . . . . . : 172.17.0.1
子网掩码 . . . . . . . . . . . . : 255.255.240.0
默认网关. . . . . . . . . . . . . :
- 在Docker Desktop中,设置图标 → Resources → Proxies,打开Manual proxy configuration,在两个输入框中输入刚刚wsl网卡的ip地址,例如
http://172.17.0.1:10809,其中端口为代理端口 - 每次重启电脑,ip地址会变
- 重要 一定要在WSL Settings里关闭自动代理功能
如果上面无效果,那就把Docker Desktop设置里的proxies关闭,给cmd命令行设置代理
set http_proxy=http://172.26.112.1:10809
set https_proxy=http://172.26.112.1:10809
set all_proxy=socks5://172.26.112.1:10809
此时用cmd命令行执行docker命令,应该就可以编译了
记得Dockerfile里也要设置代理,就设置为上文提到过的host.docker.internal相关内容
在另一个WSL2 Linux(如Ubuntu-22.04)中使用Docker Desktop
- 现在最新Windows系统可以直接用
wsl --install
安装wsl2,如果遇到其它问题,可以去网上找找方法
2. 去微软商店下载Ubuntu
下载安装后想要迁移到其它路径,那就使用
# 查看已安装列表
wsl --list
wsl --manage Ubuntu-22.04 --move <path>
注意只有WSL 2.3.11以上版本可以使用新增的迁移指令,如果是老版本或者有其它问题可以自行搜索
WSL相关操作,目前看到最前沿的教学是这个
Windows 11:WSL 2 安装和管理指南,3 种方法任你选 - 系统极客
- 记得跟上面教程一样, 一定要在WSL Settings里关闭自动代理功能
这个自动代理只适用于Mirrored模式(桥接模式),不适合Nat模式
为了每次启动命令行及系统重启后,可以让机子自动连接到代理 - 进入WSL Ubuntu并且编辑
~/.bashrc文件
在末尾添加
# 设置系统代理为 route 的 IP 地址:端口
proxy_port=10809
route_ip=$(ip route | grep default | awk '{print $3}')
if [[ -n "$route_ip" ]]; then
export http_proxy="http://${route_ip}:${proxy_port}"
export https_proxy="http://${route_ip}:${proxy_port}"
export no_proxy="localhost,127.0.0.1,::1"
echo "代理已设置为 $route_ip:${proxy_port}"
else
echo "未找到 route 的 IP 地址,未设置代理"
fi
就可以每次打开新窗口或重启都可以自动设置代理了
- 在Docker Desktop的设置图标 → Resources → WSL integration → 勾选多选框并打开对应发行版的开关即可
现在就可以在WSL2里调用Docker Desktop的docker指令了(虽然docker容器操作实际上还是在Docker Desktop里那个wsl镜像里执行的)
上面的两种基于Docker Destop的方法,本质没有区别,只是执行docker指令的地方和Dockerfile的位置(一个在Linux终端执行命令,Dockerfile在Linux目录里,一个在Window命令行执行命令,Dockerfile在Windows目录里)不一样而已
其它指令
# 构建并将结果输出到out目录
docker buildx build --load --output type=local,dest=./out .
docker buildx build --output type=local,dest=./out .
# 清除构建临时缓存
docker system prune
docker builder prune