V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Albertcord
V2EX  ›  程序员

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

  •  
  •   Albertcord · 2023-02-05 10:27:33 +08:00 · 1976 次点击
    这是一个创建于 417 天前的主题,其中的信息可能已经有所发展或是发生改变。

    之前在 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
    
    第 1 条附言  ·  2023-02-05 17:53:23 +08:00
    我现在有些怀疑是 dns 也有问题,现在的问题是 cannot resolve host raw.githubxxxx.com
    google 了几个帖子,改了 docekr 下一些配置,什么 daemon.json 配置 dns 啊,还有 /etc/resolv.conf 配置 8.8.8.8 也还是不行,看的是这个[issue]( https://github.com/microsoft/WSL/issues/8365)
    对于 wsl ,只要不碰网络的东西用起来还是愉快的,一碰到就是一地鸡毛,还没有个解决办法!难受啊
    第 2 条附言  ·  2023-02-05 19:48:03 +08:00
    应该是走代理了,通过配置~/.docker/config.json 里 wsl 访问 host 宿主机代理
    ```
    "proxies": {
    "default": {
    "httpProxy": "http://172.28.80.1:10010",
    "httpsProxy": "http://172.28.80.1:10010",
    "noProxy": "localhost"
    }
    }
    ```
    再根据 @zed1018 所说,修改 windows 下 hosts 配置,配置 raw.github 的 ip
    就能访问到了,只是又出现 EOF 的问题,还在 google 解决中,哈哈
    24 条回复    2023-02-06 21:35:16 +08:00
    zed1018
        1
    zed1018  
       2023-02-05 10:31:24 +08:00
    试试 proxychains-ng 呢
    Albertcord
        2
    Albertcord  
    OP
       2023-02-05 10:54:07 +08:00
    @zed1018 安装了(说实话还不知道这个库解决了什么问题,网络连接问题?),但是试了下还是一样的问题,但确实是不显示 443 状态码问题了,而是
    Could not resolve host: raw.githubusercontent.com
    报上面这个错误
    lcdtyph
        3
    lcdtyph  
       2023-02-05 11:03:12 +08:00 via iPhone
    链接是 dockerd 发起的,你要给 dockerd 配置代理。docker 只是 dockerd 的前端
    zed1018
        4
    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
        5
    Diphia  
       2023-02-05 11:48:53 +08:00
    应该是要在 /etc/systemd/system/docker.service.d/proxy.conf 里面配 dockerd 的 proxy
    hopingtop
        6
    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
        7
    Albertcord  
    OP
       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
        8
    Albertcord  
    OP
       2023-02-05 16:33:53 +08:00
    @Diphia 这里是指的开机启动里的 docker 配置?可是我也在~/.docker/config.json 里配置了,应该一样的吧?
    Albertcord
        9
    Albertcord  
    OP
       2023-02-05 17:49:28 +08:00
    @zed1018 试了下也还是不行
    我现在觉得不是代理的问题了,是 wsl 的 dns 问题,在 /etc/resolv.conf 里将 nameserver 改成 8.8.8.8 也还是不行,但是就是很懵,为什么我命令行里可以,程序里的命令执行 docker 就不行
    zed1018
        10
    zed1018  
       2023-02-05 17:52:23 +08:00   ❤️ 1
    @Albertcord 你改成 8888 不代表上游不会劫持,不如试试改 /etc/hosts
    Albertcord
        11
    Albertcord  
    OP
       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
        12
    zed1018  
       2023-02-05 18:53:56 +08:00   ❤️ 1
    @Albertcord 试试放 windows 下呢,不知道你用的是不是 docker desktop ,执行的时候好像是另一个 VM 不是你当前的 WSL
    zed1018
        13
    zed1018  
       2023-02-05 18:54:37 +08:00
    @zed1018 放完以后需要整个 wsl shutdown
    Albertcord
        14
    Albertcord  
    OP
       2023-02-05 19:18:03 +08:00
    @zed1018 用的是 docker desktop ,我改的是 wsl 下的 hosts ,我试下改 windows 的
    Albertcord
        15
    Albertcord  
    OP
       2023-02-05 19:19:03 +08:00
    @zed1018 有 shutdown ,有几次 shutdown 之后启动不了 wsl ,还要重启 windows 宿主机
    Albertcord
        16
    Albertcord  
    OP
       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
        17
    swcat  
       2023-02-06 08:31:26 +08:00   ❤️ 1
    所有的类虚拟机里面的代理, 都推荐用 tun2socks
    虚拟机新建一张网卡, 设置为默认, 这张网卡的所有流量都转发到 host 的 7890(clash 代理地址)
    虚拟机里面再也不用折腾什么设置代理了
    hopingtop
        18
    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
        19
    lysS  
       2023-02-06 11:52:39 +08:00
    julyclyde
        20
    julyclyde  
       2023-02-06 14:42:36 +08:00
    真正干活是 daemon 而不是 cli
    环境变量需要影响 daemon 才可以
    Albertcord
        21
    Albertcord  
    OP
       2023-02-06 21:30:21 +08:00
    @swcat 还是刚知道这个库,我得后面花时间研究下,网络上没找到什么资源,如果确实能一劳永逸,那么我也抽时间写个文章发在知乎上
    Albertcord
        22
    Albertcord  
    OP
       2023-02-06 21:33:46 +08:00
    @hopingtop
    果然是 go 内置模块的执行过程就是绕过 shell 的,真让人折腾啊!
    我现在是通过找 wsl 自动生成的 resolv.conf 里的 nameserver 机器,区分 host ip 跟 wsl ip ,然后在 wsl 里设置 env ,设置代理到 host_ip:clash_port ,来走代理,感觉跟你所说的处理方法相似
    Albertcord
        23
    Albertcord  
    OP
       2023-02-06 21:34:36 +08:00
    @julyclyde 是的,但是我是有配置 daemon 的,但是 go 执行 cmd 不走 wsl 的 shell 配置
    Albertcord
        24
    Albertcord  
    OP
       2023-02-06 21:35:16 +08:00
    @lysS env 是环境变量吧,这里是要让 cmd 执行在某个 shell 里,我看看有没有类似的参数
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5482 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 08:23 · PVG 16:23 · LAX 01:23 · JFK 04:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.