105

我正在按照fig 指南将 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对于任何包的任何命令,我都会遇到同样的问题。

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

4

18 回答 18

69

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

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

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

# Google IPv4 nameservers nameserver 8.8.8.8 nameserver 8.8.4.4

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

但是,此更改不会是永久性的(请参阅此线程)。使其永久化: $ 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.修改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.运行Docker时使用参数

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

于 2015-08-04T10:32:51.513 回答
47

我需要将 --network=host 添加到我的 docker build 命令中:

docker build --network=host -t image_name .
于 2019-02-05T21:25:57.510 回答
25

我有同样的问题,它困扰了我一段时间,我在网上尝试了很多解决方案,但无济于事。但是我最终解决如下:

跑步:

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
    
于 2018-04-03T10:22:45.513 回答
17

对我来说,只需重新启动 docker daemon 就有帮助。

service docker restart
于 2016-05-13T09:31:31.820 回答
16

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

这是我的解决方案:

docker-machine restart <machine-name>
于 2016-02-08T19:41:00.483 回答
10

如果有人正在使用 docker-compose 阅读此内容。我设法通过更改我的 yaml 文件来解决这个问题,如下所示

version: 3.4
service: my-app
  build:
  context: .
  network: host

相当于写

docker build . --network host
于 2019-06-04T07:39:50.827 回答
8

对于 Ubuntu 用户

您需要在 docker config 中添加新的 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

应该管用。

于 2017-04-03T07:31:20.227 回答
6

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

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

  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

于 2017-11-16T01:15:14.987 回答
6

就我而言,docker version 1.13.0我不得不稍微修改一下docker-machine 0.9.0TanzahoUbuntu 16.04的答案(2. Modifying Docker config),如下所示:

  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
    
于 2017-02-01T20:45:54.547 回答
5

对我来说,这是因为我在 VPN 上,而 docker 找不到我的私人 PYPI 的路线。如果您需要继续使用 VPN,请使用docker build --network=host

于 2021-07-14T20:13:39.593 回答
4

作为 Docker 新手,当我在以下位置学习 Docker 教程时,我遇到了一个以这种方式表现出来的问题:

https://docs.docker.com/get-started/part2

我在公司局域网上使用 Docker 17.03.1-ce。

我检查并仔细检查了我的 DNS 设置。我使用了各种方法来配置我在 Internet 上搜索时发现的 DNS。有些在启动时导致错误。我最终决定配置 DNS 的方法是上述链接的 Linux 故障排除部分中的一种,其中 DNS 是通过 /etc/docker 目录中的 daemon.json 文件配置的。

但是,我仍然有同样的问题。最终为我解决问题的是通过 http_proxy 和 https_proxy 环境变量配置代理。我在 Dockerfile 中指定了它们,但在 RUN pip 命令之前我忽略了这样做。

尽管这似乎是一个 DNS 问题,但将这些 ENV 命令移到 RUN 命令之前对我来说有所不同。如果这对有此问题的任何人都有帮助。

于 2018-06-01T16:05:49.760 回答
3

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

于 2015-02-23T06:54:26.733 回答
3

对我来说,这是连接到我的大学 VPN 造成的。断开连接“解决了”问题。

于 2018-12-17T12:14:01.333 回答
3

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

所以,我在下面编辑 Dockerfile

RUN pip install -r /app/requirements.txt --proxy=http://user:pass@addr:port
于 2018-02-14T13:28:51.947 回答
1

我是 Docker 新手,尝试了这里提到的所有方法,但仍然没有做对。Docker版本是18,ubuntu版本是16。我试过这个方法:-首先我用公司的互联网构建docker。这个网络阻止了一些网站或一些事情在这里进展不顺利。所以其次,我连接到我自己的网络(例如,我在手机中使用)并尝试了。事情进展顺利。成功安装requirement.txt,构建docker。

于 2018-06-19T07:50:37.847 回答
1

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

跑步:$ Drill @8.8.8.8 www.amazon.com 或 @10.0.0.2 证实了这一点。

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

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

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

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

Restart docker: sudo service docker restart
于 2018-02-15T20:19:49.533 回答
0

我猜您试图在不允许从公共存储库直接访问/安装的私有环境中运行 pip install 。如果是这种情况,您可以将 --index-url 和 --trusted-host 添加到 requirements.txt 中,如下所示:

要求.txt:

--index-url https://pypi.internal.org/api/pypi/org.python.pypi/simple
--trusted-host pypi.internal.org pypi.python.org pypi.org files.pythonhosted.org
blinker==1.3
于 2021-07-14T00:51:58.160 回答
-2

让它运行。有时 pypi 会遇到连接问题,这些问题会让你觉得它坏了。只是为了确定,让它滚动,你可能会发现它自己解决了。

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

$ 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
于 2016-06-01T05:18:37.773 回答