繁体   English   中英

在Docker中安装Python库

[英]Install Python libraries in Docker

我需要在Docker中实现一个简单的python应用程序。 我遵循了指令: https : //docs.docker.com/get-started/part2/#dockerfile

我运行这样的构建命令:

sudo docker build -t sender .

我的requirements.txt看起来如下:

 pika==0.11.2

Dockerfile包含以下内容(来自上面教程的代码)

# Use an official Python runtime as a parent image
FROM python:3

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
ADD . /app

# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

当我运行它时,pip无法安装pika:

 sudo docker build -t sender .
Sending build context to Docker daemon  4.096kB
Step 1/7 : FROM python:3
 ---> 336d482502ab
Step 2/7 : WORKDIR /app
 ---> Using cache
 ---> 9b0ffaad3d8c
Step 3/7 : ADD . /app
 ---> Using cache
 ---> 42aa7eb4ab74
Step 4/7 : RUN pip install --trusted-host pypi.python.org -r requirements.txt
 ---> Running in 24a3943a217b
Collecting pika==0.11.2 (from -r requirements.txt (line 1))
  Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7f9911830668>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution',)': /simple/pika/

我尝试安装numpy而不是pika,它有同样的问题。 然后我用谷歌搜索并了解到问题出在防火墙上。

首先,我尝试像这样运行( https://github.com/docker/compose/issues/2111 ):

 sudo docker build --build-arg HTTP_PROXY=$HTTP_PROXY -t sender .

然后我试图关闭代理:

sudo ufw disable

我也尝试从Dockerfile中删除requirements.txt文件,并用pip install pika代替它。

没有任何帮助。

解决此问题的直接方法是使用主机网络模式进行构建。 这将使容器在构建时使用主机扭曲堆栈:

docker build --network=host ...

您确定docker使用的是正确的DNS服务器吗? 尝试使用以下参数运行--dns 8.8.8.8 :-- --dns 8.8.8.8

对于docker build,使用以下命令将文件resolv.conf添加到Dockerfile的目录中

nameserver 8.8.8.8
nameserver 8.8.4.4

然后将您的Dockerfile更改为

# Use an official Python runtime as a parent image
FROM python:3

# Set the working directory to /app
WORKDIR /app

# Copy resolv.conf
ADD resolv.conf /etc/resolv.conf

# Copy the current directory contents into the container at /app
ADD . /app

# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM