在Docker中安装Python库(Install Python libraries in Docker)

编程入门 行业动态 更新时间:2024-10-23 16:14:58
在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替换它。

没什么帮助。

I need to implement a simple python application inside Docker. I followed the instruction: https://docs.docker.com/get-started/part2/#dockerfile

I run a build command like that:

sudo docker build -t sender .

My requirements.txt looks the following way:

pika==0.11.2

And Dockerfile contains following (code from the tutorial above)

# 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"]

When I run it, pip can not install 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/

I tried to install numpy instead of pika, it had the same problem. Then I googled and understood that the problem is with firewall.

Firstly I tried running like that (https://github.com/docker/compose/issues/2111):

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

Then I tried to turn off proxy:

sudo ufw disable

Also I tried to throw off requirements.txt from Dockerfile and substitute it with pip install pika.

Nothing helped.

最满意答案

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

docker build --network=host ...

A direct way to solve this issue is to build using the host network mode. This will make the container use the host newtorking stack when building:

docker build --network=host ...

更多推荐

本文发布于:2023-07-31 01:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340546.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Python   Docker   libraries   Install

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!