【问题标题】:Can't install pip packages inside a docker container with Ubuntu无法使用 Ubuntu 在 docker 容器中安装 pip 包
【发布时间】:2015-04-24 10:42:06
【问题描述】:

我正在关注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 命令都会遇到同样的问题。

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

【问题讨论】:

  • 你的 docker 容器中的 pip 版本是多少?您是否在无花果上下文之外尝试过docker run mycontainer pip install -r requirements.pip?不过,这听起来像是一个 pip 版本的问题。对于一些较新的 pip 版本,您需要在运行 pip install 时指定更多命令行选项,以便找到某些包。
  • 我的 dockerfile 有 FROM python:2.7 所以它将是 python 2.7.9 附带的任何版本的 pip。我没有尝试单独运行 pip 命令,但那是因为我什至无法构建容器,因为 pip 不工作。我可能会尝试制作一个空的 python 容器并在有机会时对其进行测试
  • 也许你可以试试pip install --no-use-wheel --allow-all-external -r requirements.pip,或者如果这没有帮助,试试pip install --no-use-wheel --allow-all-external --allow-unverified blinker -r requirements.pip。过去,当我与pip install 发生类似错误时,这对我有用。您也可以尝试从 DockerFile 中删除该行,然后在没有它的情况下构建容器以测试该命令是否可以在之后执行。尽管如此,我认为这不是问题所在。
  • 您可以在运行“docker build -t .--network=host”时添加参数

标签: python docker pip fig


【解决方案1】:

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

1。将 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-server 的行: prepend domain-name-servers 8.8.8.8, 8.8.4.4;

重启 dhclient : $ sudo dhclient.

2。修改 Docker 配置

作为explained in the docs:

在桌面上运行 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

【讨论】:

  • 我在 docker-compose 中设置了 dns,但还是不行。有什么想法吗?
  • 好的,重新启动我的 docker-machine 正在解决问题。谢谢
  • 在将 Google DNS 添加到本地配置但未在 Docker 配置文件中指定 DNS 服务器后,它对我有用(如果我这样做,它就不起作用)。我正在使用带有 Docker 1.12.2 的 Ubuntu 14.04
  • 据我所知,--dns 标志仅在 docker 命令上可用。我还找不到 Docker-Compose 的等价物。
【解决方案2】:

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

docker build --network=host -t image_name .

【讨论】:

  • 这对我有用,无需更改我的 Ubuntu 19.10 VM(使用在 Window 10 上运行的 Hyper-V 快速创建创建)中的任何内容
  • 也为我在 Linux AWS 工作区上运行 Docker 工作
  • 在 Fedora 34 中为我工作
【解决方案3】:

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

跑步:

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-ce 的问题。注意:我没有文件daemon.json。可能在安装过​​程中默认不会创建该文件,您需要手动创建它。
  • 这是在 Ubuntu 18.04.1 上唯一对我有用的东西。我可以从 nmcli 命令中找到 2 个 DNS,并将它们都包含在 daemon.json 中。
  • 这是在 Ubuntu 18.04.2 上唯一对我有用的东西。该解决方案是由 dekauliya 的回答在本文前面提出的
  • 成功了。此外,此解决方案的侵入性也比其他解决方案少。谢谢!
  • 在 CentOS 7 上工作。谢谢!!
【解决方案4】:

对我来说,只需重新启动 docker daemon 就可以了。

service docker restart

【讨论】:

    【解决方案5】:

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

    这是我的解决方案:

    docker-machine restart <machine-name>
    

    【讨论】:

    • 对于那些使用 Windows 并且不确定 参数应该是什么的人:我刚刚打开一个 PowerShell 并运行 docker-machine restart,然后运行 ​​docker-machine env(因为它提示我) 并解决了问题。
    【解决方案6】:

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

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

    相当于写

    docker build . --network host
    

    【讨论】:

      【解决方案7】:

      对于 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
      

      应该可以。

      【讨论】:

      • 这是唯一对我有用的解决方案。 Ubuntu 16.04,Docker 17.03.1-ce。
      • 在 Ubuntu 16.04 上工作没有其他帮助。
      • 在此之后我无法启动 docker:docker.service: Failed with result 'exit-code'。
      • 10.252.252.252和10.253.253.253有什么特别的地方吗?我正在运行 Ubuntu 16.04 Docker 17.09.0-ce 这不起作用
      • 不适用于 Ubuntu 18.04.2 LTS 和 Docker 18.06.1-ce
      【解决方案8】:

      在我的情况下,docker version 1.13.0 和 docker-machine 0.9.0 在 Ubuntu 16.04 下,我不得不稍微修改 Tanzaho 的答案(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
        

      【讨论】:

        【解决方案9】:

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

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

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

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

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

        【讨论】:

        【解决方案10】:

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

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

        我在公司 LAN 上使用 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 命令之前对我来说有所不同。如果这对遇到此问题的人有帮助。

        【讨论】:

        • 这是唯一对我有用的解决方案。谢谢!!
        【解决方案11】:

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

        【讨论】:

          【解决方案12】:

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

          【讨论】:

            【解决方案13】:

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

            所以,我在下面编辑 Dockerfile

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

            【讨论】:

            • 它在我的情况下有效。有必要从代理选项中删除 [user:passwd@],根据 pip 文档,这是可选的: --proxy :以 [user:passwd@]proxy.server:port 的形式指定代理。跨度>
            【解决方案14】:

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

            【讨论】:

              【解决方案15】:

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

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

              为了找到可以工作的 DNS,我运行了: $ 钻 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
              

              【讨论】:

              • 我处于同样的情况,但该解决方案似乎对我不起作用。我的容器仍然无法连接到外部互联网
              【解决方案16】:

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

              【讨论】:

                【解决方案17】:

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

                requirements.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
                

                【讨论】:

                  【解决方案18】:

                  让它运行。有时 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
                  

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 2022-11-14
                    • 2020-05-15
                    • 2021-04-15
                    • 2020-08-28
                    • 2017-12-18
                    • 2020-07-26
                    • 2023-04-09
                    • 2017-11-01
                    相关资源
                    最近更新 更多