无法在 Ubuntu 的 docker 容器中安装 pip 包 [英] Can't install pip packages inside a docker container with Ubuntu

查看:44
本文介绍了无法在 Ubuntu 的 docker 容器中安装 pip 包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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/

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

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

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

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 容器之外安装东西,即使使用那个确切的包(blinker==1.3)也能正常工作,所以这不是问题.此外,此问题并非特定于该软件包.对于任何包的任何 pip install 命令,我都遇到了同样的问题.

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 install command for any package.

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

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

推荐答案

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

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

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

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

# Google IPv4 域名服务器名称服务器 8.8.8.8名称服务器 8.8.4.4

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

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

但是,此更改不会是永久性的(请参阅 这个线程).使其永久化:<代码>$ sudo nano/etc/dhcp/dhclient.conf取消注释并编辑前面加上 domain-name-server 的行:前置域名服务器 8.8.8.8, 8.8.4.4;

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;

重启 dhclient : $ sudo dhclient.

Restart dhclient : $ sudo dhclient.

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

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

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.

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

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

3.运行 Docker 时使用参数

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

这篇关于无法在 Ubuntu 的 docker 容器中安装 pip 包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆