Python 无法在 Ubuntu 的 docker 容器中安装 pip 包

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28668180/
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-08-19 03:35:14  来源:igfitidea点击:

Can't install pip packages inside a docker container with Ubuntu

pythondockerpipfig

提问by Migwell

I'm following the fig guideto using docker with a python application, but when docker gets up to the command

我正在按照fig 指南将 docker 与 python 应用程序一起使用,但是当 docker 执行命令时

RUN pip install -r requirements.txt

I get the following error message:

我收到以下错误消息:

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/

This repeats several times and then I get another message:

这会重复几次,然后我收到另一条消息:

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))

So for some reason pip can't access any packages from inside a docker container. Is there anything I need to do to allow it internet access?

因此,出于某种原因,pip 无法从 docker 容器内访问任何包。我需要做些什么才能允许它访问互联网?

However pip works fine to install things outside of the docker container, and worked fine even with that exact package (blinker==1.3) so that's not the problem. Also this problem isn't specific to that package. I get the same issue with any pip installcommand for any package.

但是 pip 可以很好地在 docker 容器之外安装东西,即使使用那个确切的包 ( blinker==1.3) 也能正常工作,所以这不是问题。此外,此问题并非特定于该软件包。pip install对于任何包的任何命令,我都会遇到同样的问题。

Does anyone have any idea what's going on here?

有谁知道这里发生了什么?

回答by Antti Haapala

I do not know the reason, but the error means that pip is trying to resolve the /simple/blinker/as a DNS hostname instead of the pypi.python.orgpart, which seems very odd since I cannot even come up with any URL for which urlparsecould return such a string as a hostname part. I'd check if there is some problem with ~/.pip/pip.conf

我不知道原因,但错误意味着 pip 试图将 解析/simple/blinker/为 DNS 主机名而不是pypi.python.org部分,这看起来很奇怪,因为我什至无法想出任何urlparse可以返回这样的字符串作为主机名的URL部分。我会检查是否有问题~/.pip/pip.conf

回答by Tanzaho

Your problem comes from the fact that Docker is not using the proper DNS server. You can fix it in three different ways :

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

1. Adding Google DNS to your local config

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

Modifying /etc/resolv.conf and adding the following lines at the end

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

# Google IPv4 nameservers nameserver 8.8.8.8 nameserver 8.8.4.4

# Google IPv4 nameservers nameserver 8.8.8.8 nameserver 8.8.4.4

If you want to add other DNS servers, have a look here.

如果要添加其他 DNS 服务器,请查看此处

However this change won't be permanent (see this thread). To make it permanent : $ sudo nano /etc/dhcp/dhclient.conf Uncomment and edit the line with prepend domain-name-server : prepend domain-name-servers 8.8.8.8, 8.8.4.4;

但是,此更改不会是永久性的(请参阅此线程)。使其永久化: $ sudo nano /etc/dhcp/dhclient.conf 取消注释并编辑前面加上 domain-name-server 的行: prepend domain-name-servers 8.8.8.8, 8.8.4.4;

Restart dhclient : $ sudo dhclient.

重新启动 dhclient : $ sudo dhclient

2. Modifying Docker config

2. 修改 Docker 配置

As explained in the docs:

文档中所述

Systems that run Ubuntu or an Ubuntu derivative on the desktop typically use 127.0.0.1 as the default nameserver in /etc/resolv.conf file.

To specify a DNS server for use by Docker :

在桌面上运行 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. Using a parameter when you run Docker

3. 运行Docker时使用参数

When you run docker, simply add the following parameter : --dns 8.8.8.8

运行 docker 时,只需添加以下参数: --dns 8.8.8.8

回答by orluke

ok, restarting my docker-machine is solving the problem. thanks – ismailsunni

好的,重新启动我的 docker-machine 正在解决问题。谢谢 – ismailsunni

This was the solution for me:

这对我来说是解决方案:

docker-machine restart <machine-name>

回答by Bartoszer

For me simply restarting docker daemon helped.

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

service docker restart

回答by John Mee

Let it run. Sometimes pypi is having connection issues which are noisily put in your face to make you think it is broke. Just to be sure, let it roll, you might find it works it out for itself.

让它运行。有时 pypi 会遇到连接问题,这些问题会大声地放在您的脸上,让您认为它已损坏。可以肯定的是,让它滚动,你可能会发现它自己解决了。

The bottom line, despite these red error lines, is "Successfully built"

尽管有这些红色错误线,但底线是“已成功构建”

$ 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

回答by vabada

In my case, with docker version 1.13.0and docker-machine 0.9.0under Ubuntu 16.04I had to modify slightly Tanzaho's answer (2. Modifying Docker config) as follows:

在我的情况下,docker version 1.13.0docker-machine 0.9.0Ubuntu 16.04我不得不修改略有Tanzaho的答案(2修改泊坞窗配置),如下所示:

  1. Log into Ubuntu as a user with sudo privileges.

  2. Open the /etc/default/docker file for editing:

    sudo vim /etc/default/docker
    
  3. Add the following setting for Docker.

    DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4"
    
  4. Save and close the file.

  5. Restart the Docker daemon :

    sudo service docker restart
    
  1. 以具有 sudo 权限的用户身份登录 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. 保存并关闭文件。

  5. 重启 Docker 守护进程:

    sudo service docker restart
    

回答by Broski

For Ubuntu users

对于 Ubuntu 用户

You need to add new DNS addresses in the docker config

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

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

Add the dns after ExecStar.

在 ExecStar 之后添加 dns。

--dns 10.252.252.252 --dns 10.253.253.253

Should look like that:

应该是这样的:

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

Then do:

然后做:

systemctl daemon-reload
sudo service docker restart

Should work.

应该管用。

回答by dekauliya

For me, I was unable to install pip due to the docker's DNS not configured properly. I've tried the above steps, however, configuring docker DNS to Google DNS does not work for my laptop. Docker's DNS can be properly configured only if I set its DNS to my laptop's assigned IP.

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

If you use Ubuntu, you can use the following steps to configure your docker's DNS:

如果您使用 Ubuntu,您可以使用以下步骤来配置您的 docker 的 DNS:

  1. Find out your device's assigned IP. You can find this by either

    • Checking the inet addr of your ethernet or wlan in ifconfig
    • Choosing any address in nmcli dev show | grep 'DNS'
  2. Edit dns in /etc/docker/daemon.json(create this file if it doesn't exist previously)

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

  1. 找出您设备的分配 IP。你可以通过以下任一方式找到这个

    • 检查您的以太网或 wlan 的 inet 地址 ifconfig
    • 选择任何地址 nmcli dev show | grep 'DNS'
  2. 编辑 dns /etc/docker/daemon.json(如果之前不存在则创建此文件)

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

回答by H.H

I had same problem.The cause of error is proxy.

我有同样的问题。错误的原因是代理。

So, I edit Dockerfile following

所以,我编辑 Dockerfile 如下

RUN pip install -r /app/requirements.txt --proxy=http://user:pass@addr:port

回答by Tomas

Configuring docker DNS to Google DNS (8.8.8.8) or 10.0.0.2 did not work in my company environment.

将 docker DNS 配置为 Google DNS (8.8.8.8) 或 10.0.0.2 在我公司环境中不起作用。

Running: $ drill @8.8.8.8 www.amazon.com or @10.0.0.2 confirmed this.

运行:$ 钻@8.8.8.8 www.amazon.com 或@10.0.0.2 证实了这一点。

In order to find a DNS that would work I ran: $ drill www.amazon.com and it gave me the DNS IP that is being used in my network.

为了找到一个可以工作的 DNS,我运行了:$drill www.amazon.com,它给了我在我的网络中使用的 DNS IP。

Then I set it in Ubuntu using the following step to configure docker's DNS.

然后我使用以下步骤在 Ubuntu 中设置它以配置 docker 的 DNS。

Changed dns in /etc/docker/daemon.json

更改了 /etc/docker/daemon.json 中的 dns

{
    "dns": ["the DNS ip from step1"]
}

Restart docker: sudo service docker restart