简体   繁体   English

在 Docker 容器中下载 Pip 包

[英]Downloading Pip Packages in Docker Container

I am trying to download the Pika package using Pip3 inside of a Dockerfile/container.我正在尝试在 Dockerfile/容器中使用 Pip3 下载 Pika package。 My current Dockerfile looks like this:我当前的 Dockerfile 看起来像这样:

FROM rabbitmq

#install Python
RUN apt-get update &&\
    apt-get install -y \
    python3-pip

#Create new user
RUN useradd -ms /bin/bash user
USER user
WORKDIR /home/user

#Install Pika
RUN pip3 install pika

RUN mkdir videos

COPY . .

CMD ["python3", "ffmpeg.py"]

The output I get claims that it all works and everything is installed successfully. output 我得到声称它一切正常并且一切都安装成功。 However the container exits immediately due to the error:但是容器由于错误立即退出:

Traceback (most recent call last):
File "ffmpeg.py", line 1, in <module>
import pika, sys, os
ModuleNotFoundError: No module named 'pika'

If I SSH into the container and download Pika manually using:如果我将 SSH 放入容器并使用以下命令手动下载 Pika:

pip3 install pika

And then run the python file, everything works.然后运行 python 文件,一切正常。 But for some reason the Dockerfile can't install it with the exact same command.但由于某种原因,Dockerfile 无法使用完全相同的命令安装它。

So far I've tried pretty much every solution on this page到目前为止,我已经尝试了此页面上的几乎所有解决方案

I'm running Ubuntu version 20.04.1 and Docker version 20.10.2.我正在运行 Ubuntu 版本 20.04.1 和 Docker 版本 20.10.2。

Anything else I can try?还有什么我可以尝试的吗?

So I think I found a solution.所以我想我找到了解决办法。 I changed my Dockerfile to have my Pika installation above the creation of a new user.我将我的 Dockerfile 更改为在创建新用户之前安装我的 Pika。 So it now looks like this:所以它现在看起来像这样:

FROM rabbitmq

#install Python
RUN apt-get update &&\
    apt-get install -y \
    python3-pip

#Install Pika
RUN python3 -m pip install pika

#Create new user
RUN useradd -ms /bin/bash user
USER user
WORKDIR /home/user

RUN mkdir videos

COPY . .

CMD ["python3", "ffmpeg.py"]

This has fixed my issue of Pika not being recognised but now when building the image I get this:这解决了我无法识别 Pika 的问题,但现在在构建图像时,我得到了这个:

The directory '/var/lib/rabbitmq/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.

The image still builds and the file now runs, so not sure what kind of effect the new error has.图像仍在构建并且文件现在运行,所以不确定新错误有什么样的影响。

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

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