我在企业环境中使用Windows 7上的Docker Toolbox。我的工作流程需要从一个神器中拉出容器并将它们推送到另一个神器(例如外部和内部)。每个工件都需要不同的代理才能访问它。有没有办法配置Docker守护进程根据URL选择代理?或者,如果没有,我还能做些什么来使这项工作?
由于作为Pierre B. mentioned,Docker daemon不支持基于URL的代理选择,因此解决方案是将其指向配置为基于URL选择适当的上游代理的本地代理。
虽然任何能够进行上游选择的HTTP [S]代理都能做到,(pac4cli项目特别有趣,因为它的广告功能是基于大多数Web浏览器在企业环境中使用的proxy-auto-discovery协议选择上游),我选择使用tinyproxy ,作为更成熟和轻量级的解决方案。此外,我决定在docker-machine
VM中运行我的代理,以简化其部署并确保代理在Docker守护程序需要时始终运行。
以下是我用来设置系统的步骤。我特别感谢phoenix提供steps在公司代理后面的Windows上设置Docker Toolbox,并将从该答案中大量借用。
从这一点开始,我将假设Docker Quickstart Terminal或GitBash,PATH
中的docker,作为命令行控制台,“username”是您的Windows用户名。
tinyproxy
on your target platform首先拉一个干净的Linux发行版,我使用CentOS,并在其中运行bash
:
docker run -it --name=centos centos bash
接下来,安装我们需要的工具:
yum install -y make gcc
之后我们从它的GitHub存储库中取出Tinyproxy的latest release并将其解压缩到root的主目录中(在撰写本文时,最新版本为1.10.0):
cd
curl -L https://github.com/tinyproxy/tinyproxy/releases/download/1.10.0/tinyproxy-1.10.0.tar.gz \
| tar -xz
cd tinyproxy-1.10.0
现在让我们配置和构建它:
./configure --enable-upstream \
--disable-filter\
--disable-reverse\
--disable-transparent\
--disable-xtinyproxy
make
虽然显然需要--enable-upstream
,但禁用其他默认功能是可选的,但这是一种很好的做法。要确保它实际运行:
./src/tinyproxy -h
你应该看到类似的东西:
Usage: tinyproxy [options]
Options are:
-d Do not daemonize (run in foreground).
-c FILE Use an alternate configuration file.
-h Display this usage information.
-v Display version information.
Features compiled in:
Upstream proxy support
For support and bug reporting instructions, please visit
<https://tinyproxy.github.io/>.
我们通过按Ctrl + D退出容器并将可执行文件复制到可从docker-machine
VM访问的特殊文件夹位置:
docker cp centos://root/tinyproxy-1.10.0/src/tinyproxy \
/c/Users/username/tinyproxy
用您的Windows用户名替换“username”。请注意,需要在“root”之前使用双斜杠 - //
来禁用MINGW路径转换。
现在我们可以删除容器:
docker rm centos
选择TCP端口号以运行代理。这可以是docker-machine
VM上未使用的任何端口。在这个例子中我将使用数字8618。
首先,让我们删除现有的默认Docker VM: 警告:这将永久删除所有当前存储的容器和图像
docker-machine rm -f default
接下来,我们重新创建默认机器设置HTTP_PROXY
和HTTPS_PROXY
环境变量到本地主机和我们选择的端口,然后刷新我们的shell环境:
docker-machine create default \
--engine-env HTTP_PROXY=http://localhost:8618 \
--engine-env HTTPS_PROXY=http://localhost:8618
eval $(docker-machine env)
或者,我们还可以设置NO_PROXY
环境变量来列出守护程序应该直接连接的主机和/或通配符(由;
分隔),绕过代理。
tinyproxy
inside docker-machine
VM首先,我们将在/c/Users/username
目录中创建两个文件(这是我们的tinyproxy
二进制文件应该位于上面的步骤1之后),然后我们将它们复制到VM。
第一个文件是tinyproxy.conf
,Tinyproxy网站上记录了确切的语法,但下面的示例应该具有以下所有需要的设置:
# These settings can be customized to your liking,
# the port though must be the same we used in Step 2
listen 127.0.0.1
port 8618
user nobody
group nogroup
loglevel critical
syslog on
maxclients 50
startservers 2
minspareServers 2
maxspareServers 5
disableviaheader yes
# Here is the actual proxy selection, rules apply from top
# to bottom, and the last one is the default. More info on:
# https://tinyproxy.github.io/
upstream http proxy1.corp.example.com:80 ".foo.example.com"
upstream http proxy2.corp.example.com:80 ".bar.example.com"
upstream http proxy.corp.example.com:82
在上面的例子中:
http://proxy1.corp.example.com:80
将用于连接以“foo.example.com”结尾的网址,例如http://www.foo.example.com
http://proxy2.corp.example.com:80
将用于连接以“bar.example.com”结尾的网址,例如http://www.bar.example.com
,以及http://proxy.corp.example.com:80
将用于连接所有其他URL还可以匹配没有域的确切主机名,IP地址,子网和主机。
第二个文件是作为将启动代理的shell脚本,其名称必须是bootlocal.sh
:
#! /bin/sh
# Terminate on error
set -e
# Switch to the script directory
cd $(dirname $0)
# Launch proxy server
./tinyproxy -c tinyproxy.conf
现在,让我们连接到docker VM,获取root,然后切换到boot2docker目录:
docker-machine ssh
sudo -s
cd /var/lib/boot2docker
接下来,我们将复制所有三个文件并设置其权限:
cp /c/Users/username/boot2docker/{tinyproxy{,.conf},bootlocal.sh} .
chmod 755 tinyproxy bootlocal.sh
chmod 644 tinyproxy.conf
按两次Ctrl + D退出VM会话并重新启动它:
docker-machine restart default
而已!现在docker
应该能够从不同的URL中拉出并推送图像,自动选择正确的代理服务器。