如何为 docker 配置代理以提升访问速度或解决不可访问的问题
首先需要准备一个 http 的代理服务,这个具体细节好像不能细说
1、下载核心
在 linux 环境下,可下载核心:https://github.com/v*fly/v*ray-core/releases
根据系统和架构选择对应的下载
核心兼有服务端和客户端功能,配置什么就是什么
2、配置功能
核心默认有一个 config.json 文件模板,但不能直接使用
具体 config.json 内容如果没有编写经验,可以找一个本地客户端,进行配置使用
使用连接测试没问题后,找到配置文件(路径参考:客户端根目录\guiConfigs\config.json)
3、复制配置
将上一步的复制到服务器上简单修改下(因 docker 只能使用 http 的代理服务,默认一般客户端都使用的 socks 协议生成配置,所以修改配置为 http 协议代理)
修改部分:将 inbounds 部分下的 tag、protocol 从 socks 改为 http

4、运行代理
使用以下命令运行,会占用当前命令行,即用即关吧,如有需求长期在后台运行,可以自行搜索方法。
./v*ray run
然后再说为 docker 配置代理
1、为容器镜像拉取加速
(这个其实更推荐使用镜像站,更稳定(可参考:https://www.cnblogs.com/xwgli/p/12287556.html))
配置代理可参考官方文档:https://docs.docker.com/config/daemon/systemd/#httphttps-proxy
1.1、创建并编辑配置文件
sudo mkdir -p /etc/systemd/system/docker.service.d
sudo vi /etc/systemd/system/docker.service.d/http-proxy.conf
1.2、写入以下配置
[Service]
Environment="HTTP_PROXY=127.0.0.1:10908"
Environment="HTTPS_PROXY=127.0.0.1:10908"
Environment="NO_PROXY=localhost,127.0.0.1"
1.3、重启 docker 服务
sudo systemctl daemon-reload
sudo systemctl restart docker
1.4、完成。检查配置。测试。
sudo systemctl show --property=Environment docker
2、容器运行时加速
这个就没得绕了,有两种配置方式
参考:https://docs.docker.com/network/proxy/
1、全局配置
创建或编辑:~/.docker/config.json 文件
内容修改如下:
{ "proxies": { "default": { "httpProxy": "http://192.168.1.12:3128", "httpsProxy": "http://192.168.1.12:3128", "noProxy": "*.test.example.com,.example2.com,127.0.0.0/8" } } }
此配置会在创建新容器时自动配置在新容器中
2、单个容器运行时配置,为容器配置以下环境变量即可
docker run -d \ -e HTTP_PROXY="http://192.168.1.12:3128" \ -e HTTPS_PROXY="https://192.168.1.12:3128" \ -e NO_PROXY="*.test.example.com,.example2.com" \ --name xxx xxx/xxx:latest
比较推荐这种方法,这样只需要在需要的容器中配置即可,不会导致全局都被代理
参考自:https://www.cnblogs.com/michael9/p/12300824.html
输了你,赢了世界又如何...

浙公网安备 33010602011771号