无法使用Ubuntu在Docker容器中安装pip包

我遵循无花果指南使用docker与python应用程序,但是当docker得到的命令

RUN pip install -r requirements.txt 

我收到以下错误信息:

 Step 3 : RUN pip install -r requirements.txt ---> Running in fe0b84217ad1 Collecting blinker==1.3 (from -r requirements.txt (line 1)) Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connection broken by 'ProtocolError('Connection aborted.', gaierror(-2, 'Name or service not known'))': /simple/blinker/ 

这几次重复,然后我得到另一个消息:

 Could not find any downloads that satisfy the requirement blinker==1.3 (from -r requirements.txt (line 1)) No distributions at all found for blinker==1.3 (from -r requirements.txt (line 1)) 

所以由于某些原因,pip不能从Docker容器中访问任何包。 有什么我需要做的,以允许互联网接入?

然而,点子工作正常,以便在docker集装箱以外的地方安装东西,即使使用精确的包装( blinker==1.3 ),也可以正常工作,所以这不是问题。 另外这个问题不是特定于那个包的。 任何软件包的任何pip install命令都会遇到同样的问题。

有没有人知道这里发生了什么?

你的问题来自于Docker没有使用正确的DNS服务器的事实。 您可以通过三种不同的方式解决它:

1.将Google DNS添加到您的本地configuration

修改/etc/resolv.conf并在最后添加以下行

# Google IPv4 nameservers nameserver 8.8.8.8 nameserver 8.8.4.4

如果你想添加其他的DNS服务器,看看这里 。

但是这个改变不会是永久的(参见这个主题 )。 要使其成为永久的,请执行以下$ sudo nano /etc/dhcp/dhclient.conf$ sudo nano /etc/dhcp/dhclient.conf使用prepend domain-name-server取消注释并编辑该行: prepend domain-name-servers 8.8.8.8, 8.8.4.4;

重新启动dhclient: $ sudo dhclient

2.修改Dockerconfiguration

正如文档中所解释的那样 :

在桌面上运行Ubuntu或Ubuntu派生的系统通常使用127.0.0.1作为/etc/resolv.conf文件中的默认名称服务器。

指定一个供Docker使用的DNS服务器:

 1. Log into Ubuntu as a user with sudo privileges. 2. Open the /etc/default/docker file for editing : $ sudo nano /etc/default/docker 3. Add the following setting for Docker. DOCKER_OPTS="--dns 8.8.8.8" 4. Save and close the file. 5. Restart the Docker daemon : $ sudo systemctl restart docker 

3.运行Docker时使用参数

在运行--dns 8.8.8.8 ,只需添加以下参数:– --dns 8.8.8.8

好的,重新启动我的docker机正在解决这个问题。 谢谢 – ismailsunni

对我来说这是简单的解决scheme:

docker-machine restart <machine-name>

对我来说,简单地重新启动docker守护进程是有帮助的。

 service docker restart 

在我的情况下, docker version 1.13.0docker version 1.13.0 docker-machine 0.9.0Ubuntu 16.04我不得不稍微修改Tanzaho的答案(2.修改Dockerconfiguration),如下所示:

  1. 以具有sudo权限的用户身份login到Ubuntu。

  2. 打开/ etc / default / docker文件进行编辑:

     sudo vim /etc/default/docker 
  3. 为Docker添加以下设置。

     DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4" 
  4. 保存并closures文件。

  5. 重新启动Docker守护进程:

     sudo service docker restart 

对于Ubuntu用户

您需要在Dockerconfiguration中添加新的DNS地址

sudo nano /lib/systemd/system/docker.service

在ExecStar之后添加dns。

--dns 10.252.252.252 --dns 10.253.253.253

应该看起来像这样:

ExecStart=/usr/bin/dockerd -H fd:// --dns 10.252.252.252 --dns 10.253.253.253

systemctl daemon-reload sudo service docker restart应该工作

我不知道原因,但错误意味着PIP试图解决/simple/blinker/作为DNS主机名而不是pypi.python.org部分,这似乎很奇怪,因为我什至不能拿出任何URL为此urlparse可以返回这样一个string作为主机名部分。 我会检查~/.pip/pip.conf是否有问题

让它运行。 有时pypi有连接问题,这些连接问题已经在你面前吵闹,让你觉得它已经坏了。 可以肯定的是,让它滚动,你可能会发现它自己。

底线,尽pipe这些红色的错误行,是“成功build成”

 $ docker build . Sending build context to Docker daemon 2.048 kB Step 1 : FROM docker-registry.aws.example.com:5000/cmcrc/python2:20160517120608 ---> 1e5034711aa9 Step 2 : RUN pip install prometheus-client requests ---> Running in f3c580fc93ae Collecting prometheus-client Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.HTTPConnection object at 0x7fe15a1d8610>: Failed to establish a new connection: [Errno -2] Name or service not known',)': /pypi/prometheus-client/ Retrying (Retry(total=3, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.HTTPConnection object at 0x7fe15a1d87d0>: Failed to establish a new connection: [Errno -2] Name or service not known',)': /pypi/prometheus-client/ Retrying (Retry(total=2, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.HTTPConnection object at 0x7fe15a1d8990>: Failed to establish a new connection: [Errno -2] Name or service not known',)': /pypi/prometheus-client/ Retrying (Retry(total=1, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.HTTPConnection object at 0x7fe15a1d8b50>: Failed to establish a new connection: [Errno -2] Name or service not known',)': /pypi/prometheus-client/ Retrying (Retry(total=0, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.HTTPConnection object at 0x7fe15a1d8d10>: Failed to establish a new connection: [Errno -2] Name or service not known',)': /pypi/prometheus-client/ Downloading prometheus_client-0.0.13.tar.gz Collecting requests Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.HTTPConnection object at 0x7fe159e9d4d0>: Failed to establish a new connection: [Errno -2] Name or service not known',)': /pypi/requests/ Retrying (Retry(total=3, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.HTTPConnection object at 0x7fe159e9da10>: Failed to establish a new connection: [Errno -2] Name or service not known',)': /pypi/requests/ Retrying (Retry(total=2, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.HTTPConnection object at 0x7fe159e9dc50>: Failed to establish a new connection: [Errno -2] Name or service not known',)': /pypi/requests/ Retrying (Retry(total=1, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.HTTPConnection object at 0x7fe159e9de10>: Failed to establish a new connection: [Errno -2] Name or service not known',)': /pypi/requests/ Retrying (Retry(total=0, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.HTTPConnection object at 0x7fe159e9dfd0>: Failed to establish a new connection: [Errno -2] Name or service not known',)': /pypi/requests/ Downloading requests-2.10.0-py2.py3-none-any.whl (506kB) Building wheels for collected packages: prometheus-client Running setup.py bdist_wheel for prometheus-client: started Running setup.py bdist_wheel for prometheus-client: finished with status 'done' Stored in directory: /root/.cache/pip/wheels/04/94/f5/b803b2ff65e8344e99ca99b7f7cb8194224017167809a32b78 Successfully built prometheus-client Installing collected packages: prometheus-client, requests Successfully installed prometheus-client-0.0.13 requests-2.10.0 ---> 19c5e3cfe08f Removing intermediate container f3c580fc93ae Successfully built 19c5e3cfe08f 

对我来说,由于docker的dnsconfiguration不正确,我无法安装pip。 我已经尝试了上述步骤,但不幸的是,将docker dnsconfiguration为Google DNS不适用于我的笔记本电脑。 只有当我将dns设置为我的笔记本电脑分配的IP时,才能正确configurationDocker的dns。

如果你使用Ubuntu,你可以使用以下步骤来configurationdocker的dns:

  1. 找出你分配的IP。 你可以find这个

    • ifconfig检查以太网或wlan的inet addr
    • nmcli dev list | grep 'DNS'select任何地址 nmcli dev list | grep 'DNS'
  2. /etc/docker/daemon.json更改dns

     { "dns": ["your_ip_in_step_1"] } 
  3. 重启docker: sudo service docker restart