windows 如何在docker工具箱中设置代理?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36844213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:34:41  来源:igfitidea点击:

How to set proxy in docker toolbox?

windowsproxydocker-toolbox

提问by Corinne Kubler

I have just installed docker toolbox on windows environnement (Windows 7 Pro) and I have got a network time out due to the entreprise proxy. How can I set the proxy in docker toolbox ?

我刚刚在 Windows 环境(Windows 7 Pro)上安装了 docker 工具箱,并且由于企业代理而出现网络超时。如何在 docker 工具箱中设置代理?

Thanks for your help.

谢谢你的帮助。

回答by ShuSon

I encountered the same problem. Here is my solution.

我遇到了同样的问题。这是我的解决方案。

Env:

环境:

Win7, Docker Toolbox 17.03, cmder terminal, behind enterprise proxy setting.

Win7,Docker Toolbox 17.03,cmder终端,企业代理设置后面。

Solution:

解决方案:

in C:\Program Files\Docker Toolbox, find start.sh file. add following two proxy settings:

在 C:\Program Files\Docker Toolbox 中,找到 start.sh 文件。添加以下两个代理设置:

export http_proxy="http://hostname:port/"
export https_proxy="http://hostname:port/"

At least, it works for me.

至少,它对我有用。

回答by mnhmilu

I have a similar problem for Windows 7but it was resolved by these steps :

我在Windows 7 上有类似的问题,但已通过以下步骤解决:

  • Step 1. Create a batch script C:\Program Files\Docker Toolbox\kitematic_proxy.cmd with below configuration

    set proxy=YOUR_PROXY
    SET HTTP_PROXY=%proxy%
    SET HTTPS_PROXY=%proxy% 
    for /f %%i in ('docker-machine.exe ip default') do set DOCKER_HOST=%%i
    SET NO_PROXY=%DOCKER_HOST%
    set DOCKER_HOST=tcp://%DOCKER_HOST%:2376
    cd Kitematic
    Kitematic.exe
    
  • Step 2. Open Oracle Virtual machine from the start menu , go to command prompt by clicking Show(Make sure your Oracle Vm is up and running)

  • 步骤 1. 使用以下配置创建批处理脚本 C:\Program Files\Docker Toolbox\kitematic_proxy.cmd

    set proxy=YOUR_PROXY
    SET HTTP_PROXY=%proxy%
    SET HTTPS_PROXY=%proxy% 
    for /f %%i in ('docker-machine.exe ip default') do set DOCKER_HOST=%%i
    SET NO_PROXY=%DOCKER_HOST%
    set DOCKER_HOST=tcp://%DOCKER_HOST%:2376
    cd Kitematic
    Kitematic.exe
    
  • 步骤 2. 从开始菜单打开 Oracle 虚拟机,单击显示转到命令提示符(确保您的 Oracle Vm 已启动并正在运行)

enter image description here

enter image description here

enter sudo vi /var/lib/boot2docker/profile

进入 sudo vi /var/lib/boot2docker/profile

add this lines

添加这一行

export HTTP_PROXY=http://your.proxy.name:8080
export HTTPS_PROXY=http://your.proxy.name:8080

use your proxy address & port

使用您的代理地址和端口

this link help me a lot https://github.com/docker/kitematic/wiki/Common-Proxy-Issues-&-Fixes

此链接对我有很大帮助 https://github.com/docker/kitematic/wiki/Common-Proxy-Issues-&-Fixes

Note:

笔记:

  1. Don't forget to add 192.168.99.100 ip to your proxy setting's exception list (use inetcpl.cpl)
  2. Don't forget to add HTTP_PROXY and HTTPS_PROXY to your user variable(Advance settings->Environment variables)
  3. Don't forget to restart your pc
  1. 不要忘记将 192.168.99.100 ip 添加到您的代理设置的例外列表中(使用inetcpl.cpl
  2. 不要忘记将 HTTP_PROXY 和 HTTPS_PROXY 添加到您的用户变量(高级设置->环境变量)
  3. 不要忘记重启你的电脑

回答by Mapad

Installing docker on windows 7(docker 18.09.0) behind an enterprise proxy was quite complicated for me. Here are the steps I followed:

在企业代理后面的Windows 7(docker 18.09.0)上安装docker 对我来说非常复杂。以下是我遵循的步骤:

  1. set HTTP_PROXY variable in your windows environment (HTTP_PROXY=http://your_proxy:port)
  2. install docker toolbox with installer or run in powershell as admin: choco install docker-toolbox(Warning! Don't use Docker for windows, as it targets Windows 10)
  3. ensure you don't have any previous VM created by your previous attempts ( docker-machine lsshould be empty. If not run: docker-machine rm default)
  4. run in powershell as user: docker-machine --native-ssh create -d virtualbox --engine-env HTTP_PROXY=$HTTP_PROXY --engine-env HTTPS_PROXY=$HTTPS_PROXY default.
  5. run C:\Program Files\Docker Toolbox\start.sh
  6. Now run docker pull busybox. This should work.
  1. 在 Windows 环境中设置 HTTP_PROXY 变​​量(HTTP_PROXY= http://your_proxy:port
  2. 使用安装程序安装 docker 工具箱或以管理员身份在 powershell 中运行:(choco install docker-toolbox警告!不要将 Docker 用于 Windows,因为它针对 Windows 10)
  3. 确保你没有任何先前的VM创建由以前的尝试(docker-machine ls应该是空的。如果不运行。docker-machine rm default
  4. 在PowerShell中为用户运行:docker-machine --native-ssh create -d virtualbox --engine-env HTTP_PROXY=$HTTP_PROXY --engine-env HTTPS_PROXY=$HTTPS_PROXY default
  5. C:\Program Files\Docker Toolbox\start.sh
  6. 现在运行docker pull busybox。这应该有效。

回答by and1er

I had an issue on my Windows 7 Docker toolbox installation

我的 Windows 7 Docker 工具箱安装出现问题

$ docker --version
Docker version 18.09.3, build 774a1f4eee

$ docker-compose --version
docker-compose version 1.23.2, build  1110ad01

When I tried

当我尝试

docker run hello-world

I received

我收到了

Unable to find image 'hello-world:latest' locally C:\Program Files\Docker Toolbox\docker.exe: Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while waiting headers). See 'C:\Program Files\Docker Toolbox\docker.exe run --help'.

无法在本地 C:\Program Files\Docker Toolbox\docker.exe 中找到图像“hello-world:latest”:来自守护进程的错误响应:获取https://registry-1.docker.io/v2/: net/http:等待连接时取消请求(等待标头时超出 Client.Timeout)。请参阅“C:\Program Files\Docker Toolbox\docker.exe run --help”。

According to https://docs.docker.com/toolbox/faqs/troubleshoot/I've registered my enterprise proxy in /var/lib/boot2docker/profileinside the docker machine:

根据https://docs.docker.com/toolbox/faqs/troubleshoot/我已经/var/lib/boot2docker/profile在 docker 机器内部注册了我的企业代理:

  1. Use sshto log in to the virtual machine. This example logs in to the defaultmachine.

    $ docker-machine ssh default
    docker@default:~$ sudo vi /var/lib/boot2docker/profile
    
  1. 使用ssh登录到虚拟机。本示例登录到default机器。

    $ docker-machine ssh default
    docker@default:~$ sudo vi /var/lib/boot2docker/profile
    

Then I added my enterprise proxy in the end of the profile

然后我在最后添加了我的企业代理 profile

export "HTTP_PROXY=http://host:port"
export "HTTPS_PROXY=http://host:port"

after that I continued the instructions

之后我继续说明

  1. Add a NO_PROXYsetting to the end of the file similar to the example below.

      export "NO_PROXY=192.168.*.*"
    
  2. Restart Docker. After you modify the profileon your VM, restart Docker and log out of the machine.

      docker@default:~$ sudo /etc/init.d/docker restart
      docker@default:~$ exit
    
  1. NO_PROXY设置添加到文件末尾,类似于下面的示例。

      export "NO_PROXY=192.168.*.*"
    
  2. 重启 Docker。在profileVM 上修改 之后,重新启动 Docker 并注销机器。

      docker@default:~$ sudo /etc/init.d/docker restart
      docker@default:~$ exit
    

After that docker run hello-worldcommand works well

在该docker run hello-world命令运行良好之后

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/

For more examples and ideas, visit:
https://docs.docker.com/get-started/

Configuration lost after PC restart

PC重启后配置丢失

As @rsb2097 mentioned after every PC reboot Docker Machine lose the settings in /var/lib/boot2docker/profile. I face the same problem too and I don't know how to avoid this, but I made a script to write these settings simpler.

正如@rsb2097 在每台 PC 重启后提到的 Docker Machine 会丢失/var/lib/boot2docker/profile. 我也面临同样的问题,我不知道如何避免这种情况,但我制作了一个脚本来更简单地编写这些设置。

I thought that happens because I shut down the PC without stopping the docker machine (VirtualBox says that there are active connections on shut down): supposed that it damages. I tried docker-machine stopbut it doesn't help.

我认为这是因为我在没有停止 docker 机器的情况下关闭了 PC(VirtualBox 说关闭时有活动连接):假设它损坏了。我试过了,docker-machine stop但没有帮助。

As a result I wrote AddDockerMachineProxy.cmdscript that writes the proxy settings using plink.exefrom Putty (https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html).

因此,我AddDockerMachineProxy.cmd编写了使用plink.exePutty ( https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html)编写代理设置的脚本。

Usage

用法

  1. Restart the PC.
  2. Run Docker Quickstart Terminal, I have following output:

    Starting "default"...
    (default) Check network to re-create if needed...
    (default) Windows might ask for the permission to configure a dhcp server. 
    Sometimes, such confirmation window is minimized in the taskbar.
    (default) Waiting for an IP...
    Machine "default" was started.
    Waiting for SSH to be available...
    Detecting the provisioner...
    Started machines may have new IP addresses. 
    You may need to re-run the `docker-machine env` command.
    Regenerate TLS machine certs?  
    Warning: this is irreversible. (y/n): Regenerating TLS certificates
    Waiting for SSH to be available...
    Detecting the provisioner...
    Copying certs to the local machine directory...
    Copying certs to the remote machine...
    Setting Docker configuration on the remote daemon...
    
  3. Run AddDockerMachineProxy.cmdscript (plink.exemust be in %PATH%):

    @echo off
    
    echo Was "Docker Quickstart Terminal" run after the reboot to init the machine?
    echo If not this script fails.
    pause
    
    set "exePlink=plink.exe"
    set "connectionString=-pw tcuser [email protected]"
    
    echo Profile BEFORE:
    call "%exePlink%" %connectionString% cat /var/lib/boot2docker/profile
    if errorlevel 1 ( echo ERROR: PSCP failed !!! & goto BadExit )
    
    echo APPENDING PROXY
    call "%exePlink%" %connectionString% sudo bash -c "'echo export \"HTTP_PROXY=http://host:port\">> /var/lib/boot2docker/profile'"
    if errorlevel 1 ( echo ERROR: PSCP failed !!! & goto BadExit )
    
    call "%exePlink%" %connectionString% sudo bash -c "'echo export \"HTTPS_PROXY=http://host:port\">> /var/lib/boot2docker/profile'"
    if errorlevel 1 ( echo ERROR: PSCP failed !!! & goto BadExit )
    
    call "%exePlink%" %connectionString% sudo bash -c "'echo export \"NO_PROXY=192.168.*.*\">> /var/lib/boot2docker/profile'"
    if errorlevel 1 ( echo ERROR: PSCP failed !!! & goto BadExit )
    
    echo Profile AFTER:
    call "%exePlink%" %connectionString% cat /var/lib/boot2docker/profile
    if errorlevel 1 ( echo ERROR: PSCP failed !!! & goto BadExit )
    
    echo Restart docker service:
    call "%exePlink%" %connectionString% sudo /etc/init.d/docker restart
    if errorlevel 1 ( echo ERROR: PSCP failed !!! & goto BadExit )
    
    echo Testing connection
    call docker image pull hello-world || ( echo ERROR: docker image pull is failed !!! & goto BadExit )
    
    echo Done!
    exit /b 0
    
    :BadExit
    echo ERROR !!!
    exit /b 1
    
  1. 重新启动电脑。
  2. 运行Docker Quickstart Terminal,我有以下输出:

    Starting "default"...
    (default) Check network to re-create if needed...
    (default) Windows might ask for the permission to configure a dhcp server. 
    Sometimes, such confirmation window is minimized in the taskbar.
    (default) Waiting for an IP...
    Machine "default" was started.
    Waiting for SSH to be available...
    Detecting the provisioner...
    Started machines may have new IP addresses. 
    You may need to re-run the `docker-machine env` command.
    Regenerate TLS machine certs?  
    Warning: this is irreversible. (y/n): Regenerating TLS certificates
    Waiting for SSH to be available...
    Detecting the provisioner...
    Copying certs to the local machine directory...
    Copying certs to the remote machine...
    Setting Docker configuration on the remote daemon...
    
  3. 运行AddDockerMachineProxy.cmd脚本(plink.exe必须在%PATH%):

    @echo off
    
    echo Was "Docker Quickstart Terminal" run after the reboot to init the machine?
    echo If not this script fails.
    pause
    
    set "exePlink=plink.exe"
    set "connectionString=-pw tcuser [email protected]"
    
    echo Profile BEFORE:
    call "%exePlink%" %connectionString% cat /var/lib/boot2docker/profile
    if errorlevel 1 ( echo ERROR: PSCP failed !!! & goto BadExit )
    
    echo APPENDING PROXY
    call "%exePlink%" %connectionString% sudo bash -c "'echo export \"HTTP_PROXY=http://host:port\">> /var/lib/boot2docker/profile'"
    if errorlevel 1 ( echo ERROR: PSCP failed !!! & goto BadExit )
    
    call "%exePlink%" %connectionString% sudo bash -c "'echo export \"HTTPS_PROXY=http://host:port\">> /var/lib/boot2docker/profile'"
    if errorlevel 1 ( echo ERROR: PSCP failed !!! & goto BadExit )
    
    call "%exePlink%" %connectionString% sudo bash -c "'echo export \"NO_PROXY=192.168.*.*\">> /var/lib/boot2docker/profile'"
    if errorlevel 1 ( echo ERROR: PSCP failed !!! & goto BadExit )
    
    echo Profile AFTER:
    call "%exePlink%" %connectionString% cat /var/lib/boot2docker/profile
    if errorlevel 1 ( echo ERROR: PSCP failed !!! & goto BadExit )
    
    echo Restart docker service:
    call "%exePlink%" %connectionString% sudo /etc/init.d/docker restart
    if errorlevel 1 ( echo ERROR: PSCP failed !!! & goto BadExit )
    
    echo Testing connection
    call docker image pull hello-world || ( echo ERROR: docker image pull is failed !!! & goto BadExit )
    
    echo Done!
    exit /b 0
    
    :BadExit
    echo ERROR !!!
    exit /b 1
    

回答by Anton Ovsyannikov

Ah! Actually with Docker Toolbox windows part is just very thin layer over created virtual machine, so my method is to configure virtual machine itself to make everything work. So.

啊! 实际上,使用 Docker Toolbox 窗口部分只是在创建的虚拟机上的非常薄的一层,所以我的方法是配置虚拟机本身以使一切正常。所以。

0) Set global environment variables on Windows host machine

0) 在 Windows 主机上设置全局环境变量

HTTP_PROXY = "http://login:password@yourproxy:8080"
HTTPS_PROXY = "http://login:password@yourproxy:8080"

Note caps letters! (also you can set FTP_PROXYand NO_PROXY)

注意大写字母!(你也可以设置FTP_PROXYNO_PROXY

1) Run Docker Quickstart Terminal, it will create virtual machine named defaultunder you VirtualBox or whatever. Also it will display address of your newly created VM like

1) 运行 Docker Quickstart Terminal,它将创建名为defaultVirtualBox 或其他名称的虚拟机。它还会显示您新创建的虚拟机的地址,例如

docker is configured to use the default machine with IP 192.168.99.104

docker is configured to use the default machine with IP 192.168.99.104

2) SSH to this address (i.e. with PuTTY). Login:dockerPassword:tcuser

2) SSH 到此地址(即使用 PuTTY)。登录名:docker密码:tcuser

3) Run

3) 运行

echo '
{
 "proxies":
 {
   "default":
   {
     "httpProxy": "http://login:password@yourproxy:8080",
     "httpsProxy": "http://login:password@yourproxy:8080"
   }
 }
}' > /home/docker/.docker/config.json

This will force docker client (on VM!) to run containers with correct envs inside.

这将强制 docker 客户端(在 VM 上!)运行内部具有正确 envs 的容器。

4) So now you can use docker client inside VM. To force Windows docker client (as well as docker-compose) also to set correct envs inside running containers, put the same config.jsonas in p.3 on Windows host machine to C:\User\<yourhomedir>\.dockerdirectory.

4) 所以现在你可以在 VM 中使用 docker 客户端了。要强制 Windows docker 客户端(以及 docker-compose)也在运行的容器内设置正确的 envs,请将与config.jsonWindows 主机上的 p.3 中的相同内容放到C:\User\<yourhomedir>\.docker目录中。

Now check the environment inside running container

现在检查正在运行的容器内的环境

docker run -ti ubuntu env

docker run -ti ubuntu env

HTTPS_PROXY=http://login:password@yourproxy:8080
https_proxy=http://login:password@yourproxy:8080
HTTP_PROXY=http://login:password@yourproxy:8080
http_proxy=http://login:password@yourproxy:8080

Note both CAPS and lower letter variables are set properly!

注意大写和小写字母变量都设置正确!

Final check for everything is ok:

最后检查一切正常:

docker run -ti ubuntu apt-get update

docker run -ti ubuntu apt-get update

5) One issue you may face, is that address of your proxy is from network, which docker use when creating own networks, so it will spoil route to your proxy right after you will do docker network create. So make sure the proxy address is not like 172.18.x.x . If so force docker to use another address space for created networks by making another config on VM

5) 您可能面临的一个问题是,您的代理地址来自网络,docker 在创建自己的网络时使用该地址,因此在您执行此操作后会立即破坏到您的代理的路由docker network create。所以请确保代理地址不像 172.18.xx 。如果是这样,通过在 VM 上进行另一个配置,强制 docker 为创建的网络使用另一个地址空间

sudo -i
echo '
{
  "default-address-pools": [
    {"base":"172.80.0.0/16","size":24}
  ]
}' > /etc/docker/daemon.json

Then restart dockerd /etc/init.d/docker restart

然后重启dockerd /etc/init.d/docker restart

6) Do not restart your virtual machine, pause it when needed.

6) 不要重启你的虚拟机,需要时暂停。