在 docker 文件中安装 python 包

新手上路,请多包涵

在我的 docker 文件中,我想安装 med2image python 包( https://github.com/FNNDSC/med2image )。我使用以下代码:

 FROM ubuntu:16.04
RUN apt-get update && apt-get install -y --no-install-recommends \
    python3.5 \
    python3-pip \
    && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
RUN pip install nibabel pydicom matplotlib pillow
RUN pip install med2image

但是当我想构建图像时出现以下错误:

 Downloading https://files.pythonhosted.org/packages/6f/e5/948b023c7feb72adf7dfb26d90a13c838737bbf52be704f5ddd0878e3264/med2image-1.1.2.tar.gz
Complete output from command python setup.py egg_info:
Sorry, only Python 3.5+ is supported.

----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in  /tmp/pip-install-FnNb_S/med2image/
The command '/bin/sh -c pip install med2image' returned a non-zero code: 1

我应该怎么办?!

原文由 Mehdi 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 690
2 个回答

推荐的基础镜像

正如我在评论中所建议的,您可以编写一个如下所示的 Dockerfile:

 FROM python:3

RUN pip install --no-cache-dir --upgrade pip && \
    pip install --no-cache-dir nibabel pydicom matplotlib pillow med2image
    # Note: we had to merge the two "pip install" package lists here, otherwise
    # the last "pip install" command in the OP may break dependency resolution…

CMD ["cat", "/etc/os-release"]

上面的命令示例可以在运行时 ( docker build --pull -t test . && docker run --rm -it test ) 确认此映像基于 GNU/Linux 发行版“Debian stable”。

通用 Dockerfile 模板

最后给出一个全面的答案,请注意,关于 Python 依赖项的一个好的做法是在专用文本文件 中以声明性的方式 指定它们(按字母顺序,以方便查看和更新),因此对于您的示例,您可能想要编写以下文件:

requirements.txt

 matplotlib
med2image
nibabel
pillow
pydicom

并使用以下通用 Dockerfile

 FROM python:3

WORKDIR /usr/src/app

COPY requirements.txt ./

RUN pip install --no-cache-dir --upgrade pip \
  && pip install --no-cache-dir -r requirements.txt

COPY . .

CMD ["python", "./your-daemon-or-script.py"]

更准确地说,这是 Docker 官方镜像 python 的文档中建议的方法,§. 如何使用此图像

原文由 ErikMD 发布,翻译遵循 CC BY-SA 4.0 许可协议

其他一些答案/评论建议更改您的基本映像,但如果您想保留 ubuntu 16.04,您也可以简单地指定您的 pip/python 版本以使用 pip3pip3.5 如下图所示。

 FROM ubuntu:16.04

RUN apt-get update && apt-get install -y --no-install-recommends \
    python3.5 \
    python3-pip \
    && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

RUN pip3 install nibabel pydicom matplotlib pillow
RUN pip3 install med2image

原文由 mgc 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题