Mac的Docker:使用远程上下文时构建代理设置

发布于 2025-02-13 16:20:15 字数 1760 浏览 0 评论 0原文

我有一个运行Docker的Linux系统。该系统只能通过公司代理访问Internet。

对于docker run我必须在〜/.docker/config.json中设置代理

{
  "proxies": {
    "default": {
      "httpProxy": "...",
      "httpsProxy": "...",
      "noProxy": "..."
    }
  }
}

配置要在/etc/sysconfig/docker中设置Env vars :

HTTP_PROXY="..."
HTTPS_PROXY="..."
NO_PROXY="..."

此系统正常运行,我可以运行docker Rundocker构建,都使用代理设置。

现在,我想通过Docker Context在MacOS上的本地Docker在MacOS上运行本地Docker。

我创建了一个通过SSH连接到远程主机的上下文:

docker context create remote \
   --description "remote execution" \
   --docker "host=ssh://docker.remote"

我还在本地〜/.docker/config.json中设置代理设置:

{
  "proxies": {
    "remote": {
      "httpProxy": "...",
      "httpsProxy": "...",
      "noProxy": "..."
    }
  }
}

a docker info显示使用代理,用于Docker Run也是如此,我可以从容器内访问Internet。

调用Docker build时,任何尝试访问Internet的尝试都会失败。

我还需要更改其他文件,就像Linux上的Sysconfig文件一样?


我现在确实有一个丑陋的解决方法。我发现通过设置代理 - build-arg无效地工作。为了手动设置- build-arg s,我在我的.bashrc中添加了Docker命令的覆盖。丑陋但工作。

function docker {
  if [ "$1" == "build" ] && [ "$#" -gt 1 ]; then
    echo "Building with proxy!"
    /usr/local/bin/docker "$@" \
      --build-arg http_proxy="..." \
      --build-arg https_proxy="..." \
      --build-arg no_proxy="..."
  else
    /usr/local/bin/docker "$@"
  fi
}

I have a Linux system where I run docker. This system can only access the internet via corporate proxy.

For docker run I had to set the proxy config in my ~/.docker/config.json:

{
  "proxies": {
    "default": {
      "httpProxy": "...",
      "httpsProxy": "...",
      "noProxy": "..."
    }
  }
}

To use proxy in docker build I had to set env vars in /etc/sysconfig/docker:

HTTP_PROXY="..."
HTTPS_PROXY="..."
NO_PROXY="..."

This system works fine and I can run docker run and docker build, both using the proxy settings.

Now I want to run my local docker on MacOS against this remote machine via docker context.

I created a context that connects via ssh to the remote host:

docker context create remote \
   --description "remote execution" \
   --docker "host=ssh://docker.remote"

I also set the proxy settings in my local ~/.docker/config.json:

{
  "proxies": {
    "remote": {
      "httpProxy": "...",
      "httpsProxy": "...",
      "noProxy": "..."
    }
  }
}

A docker info shows the proxy is used and for docker run that also is true and I can access the internet from within containers.

Any attempt to access the internet though fails when calling docker build.

Is there some other file I need to change, just like the sysconfig file on linux?


I do have an ugly workaround now. I found that setting the proxy via --build-arg acutally works. To not always have to set the --build-args manually, I added an override for the docker command in my .bashrc. Ugly but working.

function docker {
  if [ "$1" == "build" ] && [ "$#" -gt 1 ]; then
    echo "Building with proxy!"
    /usr/local/bin/docker "$@" \
      --build-arg http_proxy="..." \
      --build-arg https_proxy="..." \
      --build-arg no_proxy="..."
  else
    /usr/local/bin/docker "$@"
  fi
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桜花祭 2025-02-20 16:20:15

Docker向后兼容与较旧版本的设置似乎是一个问题,我在网上尝试了每个解决方案,以设置影响docker build命令的代理,这是唯一对我有用的东西:

  1. Open 故障排除设置(错误按钮)来自Docker桌面

“故障排除按钮”

  1. clean/purge data

“清洁清除数据按钮”

  1. 重置出厂默认值

“重置出厂默认值”

  1. 清除每件事, note 您将丢失全部您的本地图像,容器,卷。

  2. docker重新启动后,从设置>资源>代理,它将生效。

但是我不能冒险丢失数据的风险

不幸的是,乏味的解决方案是设置http_proxy& https_proxy在执行Docker命令之前,每个终端会话/过程的ENV变量:

MAC/Linux:

export HTTP_PROXY=...
export HTTPS_PROXY=...

Windows: Windows:Windows:

set HTTP_PROXY=...
set HTTPS_PROXY=...

It seems an issue with docker backward compatibility with older versions settings, I've tried every single solution online for setting a proxy that affects docker build command, the only thing that worked for me:

  1. Open Troubleshoot settings (bug button) from Docker Desktop

troubleshoot button

  1. Clean/Purge data

clean purge data button

  1. Optionally Reset to factory defaults

Reset to factory defaults

  1. Purge every thing, NOTE that you will lose all your local images, containers, volumes.

  2. After docker restarts set the proxy from Docker Desktop in Settings > Resources > Proxies, and it will take effect.

But I can't risk losing my data

the tedious solution unfortunately is setting HTTP_PROXY & HTTPS_PROXY env variables for every terminal session/process before executing docker commands:

Mac / Linux:

export HTTP_PROXY=...
export HTTPS_PROXY=...

Windows:

set HTTP_PROXY=...
set HTTPS_PROXY=...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文