【问题标题】:Install Python libraries in Docker在 Docker 中安装 Python 库
【发布时间】:2018-08-04 22:43:12
【问题描述】:

我需要在 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。

没有任何帮助。

【问题讨论】:

    标签: python docker pip docker-compose dockerfile


    【解决方案1】:

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

    docker build --network=host ...
    

    【讨论】:

    • 你能解释一下这个问题的原因是什么,为什么会这样解决?
    【解决方案2】:

    您确定 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"]
    

    【讨论】:

    • 不明白该放在哪里。
    • sudo docker build --dns 8.8.8.8 -t sender 。未知标志:--dns
    • --dns 用于docker run。对于docker build,使用ADD 插入/etc/resolv.conf
    • 设置/etc/resolv.conf的内容为nameserver 8.8.8.8nameserver 8.8.4.4
    • 在 Dockerfile 我添加了 ADD resolv.conf /etc/resolv.conf
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-15
    • 2016-08-20
    • 1970-01-01
    • 2022-06-21
    • 2022-12-16
    相关资源
    最近更新 更多