pip install 在 docker 中无法正常工作

pip install doesn't work correctly in docker

问题

我无法在构建映像期间安装任何包。从 docker 集线器拉取是可以的,但是当 docker 尝试使用网络构建图像时出现问题。例如,如果我尝试 运行:

$ docker build -t sample-image:latest .

... 上述命令使用的 Dockerfile 具有以下行:

RUN pip install -r requirements.txt

...然后我得到下一个错误:

Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7fdbe102e278>: Failed to establish a new connection: [Errno -2] Name or service not known',)': /simple/sanic/

Could not find a version that satisfies the requirement sanic (from -r requirements.txt (line 1)) (from versions: )
No matching distribution found for sanic (from -r requirements.txt (line 1))

... ubuntu 包更新时出现同样的错误


信息

docker 和 docker 由 docker 官方文档安装(无任何更改)。

Server Version: 17.03.1-ce
Storage Driver: aufs
Kernel Version: 4.8.0-45-generic
Operating System: Ubuntu 16.04.2 LTS
Architecture: x86_64
CPUs: 2
Total Memory: 3.763 GiB

requirements.txt:

sanic
asyncpg
asyncio
uvloop

Docker 文件:

FROM python:3.5-onbuild

WORKDIR /usr/src/app

# https://github.com/vishnubob/wait-for-it
CMD ["./run/wait-for-it.sh", "db:5432", "--", "python", "index.py"]

P.S.

我测试了更多决定并且 none 有所帮助。感谢大家的帮助:)

谢谢Salem!以下决定帮助了我:

Try to edit /etc/NetworkManager/NetworkManager.conf and comment out the dns=dnsmasq part, restart the network and docker and try again

如果 pip install 在主机上安装包时成功,但在 docker 构建期间没有成功,那么对我有用的解决方案是从主机的 /etc/pip.conf 获得 index-url出现在 docker 图片中。

示例:

如果主机的 /etc/pip.conf 包含:

  • index-url = https://example.com/blah/blah/blah

然后在安装任何 pip 包之前将以下行添加到 Dockerfile

  • RUN echo '[global]\nindex-url = https://example.com/blah/blah/blah\ntrusted-host = https://example.com' > /etc/pip.conf