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

2024-04-29 09:36:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在遵循fig guide来将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容器中访问任何包。有什么我需要做的允许它上网吗?

不过,pip可以很好地安装docker容器之外的东西,甚至可以很好地安装那个确切的包(blinker==1.3),所以这不是问题所在。这个问题也不是那个包特有的。对于任何包,任何pip install命令都会遇到同样的问题。

有人知道这是怎么回事吗?


Tags: installpipdockerrunfromtxtnone消息
3条回答

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

这就是我的解决方案:

docker-machine restart <machine-name>

我有同样的问题,它困扰了我一段时间,我在网上尝试了很多解决方案,但都没有成功。不过,我最后决定如下:

运行:

Ubuntu 16.04 
docker Server 18.03.0-ce
  1. 查找DNS服务器的地址。

    通过运行以下命令查找DNS服务器的地址:

    $: nmcli dev show | grep 'IP4.DNS'
    IP4.DNS[1]:                192.168.210.2
    
  2. 更新Docker守护进程

    /etc/docker/daemon.json.如果您还没有一个)创建docker配置文件,并将以下内容添加到文件中:

    {
        "dns": ["192.168.210.2", "8.8.8.8"]
    }
    

    数组的第一项是网络的DNS服务器,第二项是google的DNS服务器,当网络的DNS不可用时作为备用。

    保存文件,然后重新启动docker服务

    $: sudo service docker restart
    

你的问题来自Docker没有使用正确的DNS服务器。 你可以用三种不同的方法修复它:

一。将Google DNS添加到本地配置

修改/etc/resolv.conf并在末尾添加以下行

# Google IPv4 nameservers nameserver 8.8.8.8 nameserver 8.8.4.4

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

不过,这种变化不会是永久性的(请参见this thread)。使之永久: $ sudo nano /etc/dhcp/dhclient.conf 取消注释并编辑带有前置域名服务器的行: prepend domain-name-servers 8.8.8.8, 8.8.4.4;

重新启动dhclient:$ sudo dhclient

2。修改Docker配置

作为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 :

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

三。运行Docker时使用参数

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

相关问题 更多 >