Mac的Docker:使用远程上下文时构建代理设置
我有一个运行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 Run
和docker构建
,都使用代理设置。
现在,我想通过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-arg
s 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Docker向后兼容与较旧版本的设置似乎是一个问题,我在网上尝试了每个解决方案,以设置影响
docker build
命令的代理,这是唯一对我有用的东西:清除每件事, note 您将丢失全部您的本地图像,容器,卷。
docker重新启动后,从
设置>资源>代理
,它将生效。但是我不能冒险丢失数据的风险
不幸的是,乏味的解决方案是设置
http_proxy
&https_proxy
在执行Docker命令之前,每个终端会话/过程的ENV变量:MAC/Linux:
Windows: Windows:Windows:
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:Purge every thing, NOTE that you will lose all your local images, containers, volumes.
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:
Windows: