我需要在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,它有同样的问题。然后我用Google搜索并了解问题出在防火墙上。
首先我尝试像那样运行(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替换它。
没有任何帮助。
答案 0 :(得分:1)
解决此问题的直接方法是使用主机网络模式进行构建。这将使容器在构建时使用主机newtorking堆栈:
docker build --network=host ...
答案 1 :(得分:0)
您确定docker正在使用正确的DNS服务器吗?尝试使用以下参数运行docker:--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"]