简体   繁体   English

在 Dockerfile 上运行安装 python 包

[英]Run install python packages on Dockerfile

I'm new to Docker and currently trying to create a Dockerfile with installing the python packages and its libraries as shown here:我是 Docker 的新手,目前正在尝试通过安装 python 包及其库来创建 Dockerfile,如下所示:

FROM balenalib/fincm3-debian-python:latest

# RUN install_packages git
RUN apt-get update && apt-get install python \
        && apt-get install pip3 \
        apt-get install libportaudio0 libportaudio2 libportaudiocpp0 portaudio19-dev \
        pip3 install pyaudio \
        pip3 install numpy \
        pip3 install matplotlib \
        pip3 install scipy \
        pip3 install librosa \

# Set our working directory
WORKDIR /usr/src/app

COPY Recorder.py /usr/src/app

# Recorder.py will run when container starts up on the device
CMD ["python","/usr/src/app/Recorder.py"]

However, while I am trying to push this Dockerfile, the error is generated with但是,当我尝试推送此 Dockerfile 时,会生成错误

    Error: The command '/bin/sh -c apt-get update && apt-get install python          && apt-get install pip3         apt-get install libportaudio0 libportaudio2 libportaudiocpp0 portaudio19-dev            pip3 install pyaudio
pip3 install numpy              pip3 install matplotlib                 pip3 install scipy              pip3 install librosa WORKDIR /usr/src/app' returned a non-zero code: 100

Moving python packages in requirements.txt and installing python3-pip worked with python:3 base image.在 requirements.txt 中移动 python 包并安装 python3-pip 与 python:3 基本映像一起使用。

# RUN install_packages git
  RUN apt-get update \
   && apt-get install -y python \
   && apt-get install -y python3-pip

  RUN pip install -r requirements.txt

The package you are looking for is called python3-pip .您正在寻找的 package 称为python3-pip

Next, you need both && (to separate commands) and \ (to continue the command line).接下来,您需要&& (分隔命令)和\ (继续命令行)。 So, in summary, that should be:所以,总而言之,应该是:

FROM balenalib/fincm3-debian-python:latest

RUN apt-get update && apt-get install python && \
        apt-get install -y \
              python3-pip libportaudio0 libportaudio2 libportaudiocpp0 \
              portaudio19-dev && \
        pip3 install pyaudio numpy matplotlib \
             scipy librosa 

# Set our working directory
WORKDIR /usr/src/app

COPY Recorder.py /usr/src/app

# Recorder.py will run when container starts up on the device
CMD ["python","/usr/src/app/Recorder.py"]

I believe you have more than one problem in this Dockerfile, and when you put all commands together with && and \ , you don't know which one is triggering the error.我相信您在此 Dockerfile 中遇到的问题不止一个,当您将所有命令与&& and \放在一起时,您不知道是哪一个触发了错误。 I suggest splitting them for debugging purposes, when they all work then you can put then together.我建议将它们拆分用于调试目的,当它们都工作时,您可以将它们放在一起。 Once you understand each individual error is easier to check and solve them.一旦您了解了每个单独的错误,就更容易检查和解决它们。 this question has valuable info: how to install pip in docker这个问题有很有价值的信息: how to install pip in docker

Try this:尝试这个:

1- packages are triggers Y/n questions, give -y to guarantee it passes 1- 包是触发 Y/n 问题,给 -y 以保证它通过

2- using the backslashes to refer to a new command, you should use &&, backslashes refer to breaking line, you can use \ and then && 2-使用反斜杠表示新命令,你应该使用&&,反斜杠是指断行,你可以使用\然后&&

3- pip3 and libportaudio0 packages doesn't exist. 3- pip3libportaudio0包不存在。

E: Unable to locate package libportaudio0

I found out about the errors dividing the Dockerfile like this and removing the problems mentioned :我发现了像这样划分 Dockerfile 的错误并消除了提到的问题

RUN apt-get update 
RUN apt-get install python -y\
        && apt-get install python3-pip -y
RUN apt-get install libportaudio2 libportaudiocpp0 portaudio19-dev -y 
RUN pip3 install pyaudio numpy matplotlib \
             scipy librosa

If you want to put the commands together:如果要将命令放在一起:

RUN apt-get update \
        && apt-get install python -y \
        && apt-get install python3-pip -y \
        && apt-get install libportaudio2 libportaudiocpp0 portaudio19-dev -y \
        && pip3 install pyaudio numpy matplotlib \
             scipy librosa

I also suggest adding a pip requirements file, would make things cleaner.我还建议添加一个 pip 需求文件,这会让事情变得更干净。

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

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