为什么 wsl2 里用 Go 语言调 Command 模块执行 docker 命令没走代理?

2023-02-05 10:27:33 +08:00
 Albertcord

之前在 github 上提了个issue,但发现没人回答,来 v 站求助下大佬 我的问题其实就是发现,我自己在 wsl2 里执行以下命令是可以访问到的:

curl -s -S -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer

但是我在用cog,这个库的时候发现其内部构建 Docker 镜像时获取不到上面这个 raw.githubxx ,这个链接,一直报 443 ,不过我刚刚又看了 issue ,发现怎么是报 Could not resolve host: raw.githubusercontent.com 了,我有段时间没去看了,可能我已经解决代理问题了?
继续说回问题,然后我自己写了个测试 DockerFile ,发现我 wsl2 里的 Docker 构建是会走代理的,所以很疑惑问题在哪里?有 Go 大佬帮帮忙吗,也在学 Go ,但进度有些慢,本身是前端,感觉缺少了什么上下文

FROM ubuntu
RUN apt update
RUN apt install -y git
RUN apt install -y curl
RUN cd ~
# RUN git clone https://github.com/momo-lab/pyenv-install
RUN echo $(curl -s -S -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer) > ~/test.text
2014 次点击
所在节点    程序员
24 条回复
zed1018
2023-02-05 10:31:24 +08:00
试试 proxychains-ng 呢
Albertcord
2023-02-05 10:54:07 +08:00
@zed1018 安装了(说实话还不知道这个库解决了什么问题,网络连接问题?),但是试了下还是一样的问题,但确实是不显示 443 状态码问题了,而是
Could not resolve host: raw.githubusercontent.com
报上面这个错误
lcdtyph
2023-02-05 11:03:12 +08:00
链接是 dockerd 发起的,你要给 dockerd 配置代理。docker 只是 dockerd 的前端
zed1018
2023-02-05 11:10:27 +08:00
@Albertcord 安装了要在 $HOME/.proxychains/proxychains.conf 里配置你的代理。然后命令前加上 proxychains 。它会劫持流量去定向到代理(但不是一定会成功)。

配置文件参考官方的自己改一下试试: https://github.com/rofl0r/proxychains-ng/blob/master/src/proxychains.conf
Diphia
2023-02-05 11:48:53 +08:00
应该是要在 /etc/systemd/system/docker.service.d/proxy.conf 里面配 dockerd 的 proxy
hopingtop
2023-02-05 12:09:25 +08:00
上面 #3 #5 给了方案,我瞄了一眼 cog:
cog 不得行的原因是 就是 dockerd 代理限制。

cog 做的不好的一点是,他不提供额外的 docker --build-arg 参数让用户配置!

如果提供配置,你就不需要修改 dockerd 了,这样就会更灵活

```go
func Build(dir, dockerfile, imageName string, progressOutput string) error {
var args []string
if util.IsM1Mac(runtime.GOOS, runtime.GOARCH) {
args = m1BuildxBuildArgs()
} else {
args = buildKitBuildArgs()
}
args = append(args,
"--file", "-",
"--build-arg", "BUILDKIT_INLINE_CACHE=1",
"--tag", imageName,
"--progress", progressOutput,
".",
)
cmd := exec.Command("docker", args...)
cmd.Env = append(os.Environ(), "DOCKER_BUILDKIT=1")
cmd.Dir = dir
cmd.Stdout = os.Stderr // redirect stdout to stderr - build output is all messaging
cmd.Stderr = os.Stderr
cmd.Stdin = strings.NewReader(dockerfile)

console.Debug("$ " + strings.Join(cmd.Args, " "))
return cmd.Run()
}
```
Albertcord
2023-02-05 16:33:02 +08:00
@hopingtop 我也想过是不是这个原因,所以在学 go ,打算自己测试下,但是在 cli 里执行 docker 命令跟 go 里面调 Command 模块执行 docker 为啥有这个差异呢?难道是 go 里 command 调用的是另一个没有加载 docker 代理配置的 shell ?还是说没有走 shell ,就是最根本的 docker 程序,所以没有加载代理配置(~/.docker/config.json ),我根据 4 楼指引加下另一个程序配置看看
Albertcord
2023-02-05 16:33:53 +08:00
@Diphia 这里是指的开机启动里的 docker 配置?可是我也在~/.docker/config.json 里配置了,应该一样的吧?
Albertcord
2023-02-05 17:49:28 +08:00
@zed1018 试了下也还是不行
我现在觉得不是代理的问题了,是 wsl 的 dns 问题,在 /etc/resolv.conf 里将 nameserver 改成 8.8.8.8 也还是不行,但是就是很懵,为什么我命令行里可以,程序里的命令执行 docker 就不行
zed1018
2023-02-05 17:52:23 +08:00
@Albertcord 你改成 8888 不代表上游不会劫持,不如试试改 /etc/hosts
Albertcord
2023-02-05 18:17:47 +08:00
@zed1018 麻了,试了下 https://www.ipaddress.com/site/raw.githubusercontent.com 上找到 4 个 IP ,
185.199.108.133 raw.githubusercontent.com
185.199.109.133 raw.githubusercontent.com
185.199.110.133 raw.githubusercontent.com
185.199.111.133 raw.githubusercontent.com
4 个逐个测试加在 /etc/hosts 文件里,调 sudo cog build -t style-your-hair-model 都不行,还是说
#10 0.239 curl: (6) Could not resolve host: raw.githubusercontent.com
zed1018
2023-02-05 18:53:56 +08:00
@Albertcord 试试放 windows 下呢,不知道你用的是不是 docker desktop ,执行的时候好像是另一个 VM 不是你当前的 WSL
zed1018
2023-02-05 18:54:37 +08:00
@zed1018 放完以后需要整个 wsl shutdown
Albertcord
2023-02-05 19:18:03 +08:00
@zed1018 用的是 docker desktop ,我改的是 wsl 下的 hosts ,我试下改 windows 的
Albertcord
2023-02-05 19:19:03 +08:00
@zed1018 有 shutdown ,有几次 shutdown 之后启动不了 wsl ,还要重启 windows 宿主机
Albertcord
2023-02-05 19:45:34 +08:00
@zed1018 非常感谢!
这个问题解决了,但是出现另一个错,这个应该就是程序错误了
```
$ sudo cog build -t style-your-hair-model
[sudo] password for fang:
Building Docker image from environment in cog.yaml as style-your-hair-model...
[+] Building 192.4s (11/25)
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 3.24kB 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> resolve image config for docker.io/docker/dockerfile:1.2 3.3s
=> CACHED docker-image://docker.io/docker/dockerfile:1.2@sha256:e2a8561e419ab1ba6b2fe6cbdf49fd92b95912df1cf7d313c3e2230a333fdbcc 0.0s
=> [internal] load metadata for docker.io/nvidia/cuda:10.2-cudnn8-devel-ubuntu18.04 1.8s
=> [stage-0 1/19] FROM docker.io/nvidia/cuda:10.2-cudnn8-devel-ubuntu18.04@sha256:54eff7aa01217a1fb039cf5dbfa925a4bce2073b1742074c84dd83cf7af4ab11 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 49.26kB 0.0s
=> CACHED [stage-0 2/19] RUN rm -f /etc/apt/sources.list.d/cuda.list && rm -f /etc/apt/sources.list.d/nvidia-ml.list && apt-key del 7fa2af80 0.0s
=> CACHED [stage-0 3/19] RUN --mount=type=cache,target=/var/cache/apt set -eux; apt-get update -qq; apt-get install -qqy --no-install-recommends curl; rm -rf /var/ 0.0s
=> CACHED [stage-0 4/19] RUN --mount=type=cache,target=/var/cache/apt apt-get update -qq && apt-get install -qqy --no-install-recommends make build-essential li 0.0s
=> ERROR [stage-0 5/19] RUN curl -s -S -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash && git clone https://github. 187.1s
------
> [stage-0 5/19] RUN curl -s -S -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash && git clone https://github.com/momo-lab/pyenv-install-latest.git "$(pyenv root)"/plugins/pyenv-install-latest && pyenv install-latest "3.8" && pyenv global $(pyenv install-latest --print "3.8") && pip install "wheel<1":
#10 184.7 curl: (56) Unexpected EOF
#10 184.7 /bin/sh: 1: pyenv: not found
#10 184.7 Cloning into '/plugins/pyenv-install-latest'...
#10 187.1 /bin/sh: 1: pyenv: not found
------
executor failed running [/bin/sh -c curl -s -S -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash && git clone https://github.com/momo-lab/pyenv-install-latest.git "$(pyenv root)"/plugins/pyenv-install-latest && pyenv install-latest "3.8" && pyenv global $(pyenv install-latest --print "3.8") && pip install "wheel<1"]: exit code: 127
ⅹ Failed to build Docker image: exit status 1
```
swcat
2023-02-06 08:31:26 +08:00
所有的类虚拟机里面的代理, 都推荐用 tun2socks
虚拟机新建一张网卡, 设置为默认, 这张网卡的所有流量都转发到 host 的 7890(clash 代理地址)
虚拟机里面再也不用折腾什么设置代理了
hopingtop
2023-02-06 10:11:49 +08:00
#7 你猜测是对的,官方对 os/exec 包的解释
Unlike the "system" library call from C and other languages, the os/exec package intentionally does not invoke the system shell and does not expand any glob patterns or handle other expansions, pipelines, or redirections typically done by shells. The package behaves more like C's "exec" family of functions.

官方:就是有意不调取系统的 shell 所以你原本的 shell 配置和环境变量都是读取不到的。

大多数虚拟化的东西,多会虚拟一层网卡,这种虚拟网卡就会导致网上一些常规操作无法完全适用。

对于此场景我的处理办法是:让 wsl 和宿主机 win 网络互通(还可以解决很多问题)。然后所有通过 Dockerfile 设置 http_proxy 代理到 win 上,就实现了 wsl 的 VPN 作用。这样做就不用特殊去配置 docker 了

具体如何互通,可以参考 https://zhuanlan.zhihu.com/p/588329608
lysS
2023-02-06 11:52:39 +08:00
julyclyde
2023-02-06 14:42:36 +08:00
真正干活是 daemon 而不是 cli
环境变量需要影响 daemon 才可以

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/913280

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX