×

Docker Container 使用本地127.0.0.1端口代理的方法(Win10, WSL, 10809)

Legend Legend 发表于2022-12-22 12:45:47 浏览358 评论0

抢沙发发表评论

让所有的Docker Container自动使用127.0.0.1的本地端口代理(如10809),实现加速外网访问或使用内网代理的目的。

第一步 确认Docker版本

  1. docker --version

要大于 17.07

第二步 修改config.json 文件

根据Docker手册上的配置指引,将修改~/.docker/config.json文件。

注意!

Win10配置WSL后,Win的配置和Linux配置是分开的。

所以,如果使用Win的PowerShell来用docker,要编辑的是C:\Users\当前用户名\.docker\config.json

而如果使用WSL2的Linux,比如Ubuntu来用docker,要打命令编辑 vi ~/.docker/config.json

配置参考:

  1. {
      "credsStore": "desktop",
      "proxies": {
        "default": {
          "httpProxy": "http://host.docker.internal:10809",
          "httpsProxy": "http://host.docker.internal:10809"
        }
      }
    }

注意:不是 127.0.0.1,而是 host.docker.internal 。

我尝试过127.0.0.1和局域网地址都是无效的,配置了–network host也无效。

可参考进一步解释:

https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach

本方法可做为处理以下docker报错的参考方案:

  • fatal: unable to access xxx Encountered end of file

  • Failed connect to 127.0.0.1:10809; Connection refused