如何在 Alpine 上安装 matplotlib

How to install matplotlib on Alpine

正在尝试在 alpine docker 图像上安装 matplotlib。我收到一堆丑陋的消息。我是否缺少一些需要手动安装的额外先决条件?

这里是 docker 文件:

    FROM openjdk:8-jre-alpine
    RUN apk update
    RUN apk add --no-cache tesseract-ocr
    RUN echo     import numpy, matplotlib, skimage, _tkinter > test.py
    RUN apk add --no-cache python3
    RUN pip3 install --upgrade pip setuptools
    RUN apk add --no-cache py3-numpy
    RUN pip install matplotlib

以及相关的 docker 输出(类似的输出,如果我只是在 Linux 上直播)

Collecting kiwisolver>=1.0.1
 Downloading kiwisolver-1.3.1.tar.gz (53 kB)
   ERROR: Command errored out with exit status 1:
    command: /usr/bin/python3.6 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-yp9mr2j7/kiwisolver_29f6c98e09ef4d15af4bbadde3b1c2a2/setup.py'"'"'; __file__='"'"'/tmp/pip-install-yp9mr2j7/kiwisolver_29f6c98e09ef4d15af4bbadde3b1c2a2/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-t2cdn0ra
        cwd: /tmp/pip-install-yp9mr2j7/kiwisolver_29f6c98e09ef4d15af4bbadde3b1c2a2/
   Complete output (44 lines):
   WARNING: The wheel package is not available.
     ERROR: Command errored out with exit status 1:
      command: /usr/bin/python3.6 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-wheel-nil1gs35/cppy_c6bc34a322e5441da8a1a97ba7950d95/setup.py'"'"'; __file__='"'"'/tmp/pip-wheel-nil1gs35/cppy_c6bc34a322e5441da8a1a97ba7950d95/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-wb4xn65c
          cwd: /tmp/pip-wheel-nil1gs35/cppy_c6bc34a322e5441da8a1a97ba7950d95/
     Complete output (6 lines):
     usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
        or: setup.py --help [cmd1 cmd2 ...]
        or: setup.py --help-commands
        or: setup.py cmd --help
   
     error: invalid command 'bdist_wheel'
     ----------------------------------------
     ERROR: Failed building wheel for cppy
   ERROR: Failed to build one or more wheels
   Traceback (most recent call last):
     File "/usr/lib/python3.6/site-packages/setuptools/installer.py", line 126, in fetch_build_egg
       subprocess.check_call(cmd)
     File "/usr/lib/python3.6/subprocess.py", line 311, in check_call
       raise CalledProcessError(retcode, cmd)
   subprocess.CalledProcessError: Command '['/usr/bin/python3.6', '-m', 'pip', '--disable-pip-version-check', 'wheel', '--no-deps', '-w', '/tmp/tmpa3b9n_qa', '--quiet', 'cppy>=1.1.0']' returned non-zero exit status 1.
   
   The above exception was the direct cause of the following exception:
   
   Traceback (most recent call last):
     File "<string>", line 1, in <module>
     File "/tmp/pip-install-yp9mr2j7/kiwisolver_29f6c98e09ef4d15af4bbadde3b1c2a2/setup.py", line 92, in <module>
       cmdclass={'build_ext': BuildExt},
     File "/usr/lib/python3.6/site-packages/setuptools/__init__.py", line 152, in setup
       _install_setup_requires(attrs)
     File "/usr/lib/python3.6/site-packages/setuptools/__init__.py", line 147, in _install_setup_requires
       dist.fetch_build_eggs(dist.setup_requires)
     File "/usr/lib/python3.6/site-packages/setuptools/dist.py", line 676, in fetch_build_eggs
       replace_conflicting=True,
     File "/usr/lib/python3.6/site-packages/pkg_resources/__init__.py", line 766, in resolve
       replace_conflicting=replace_conflicting
     File "/usr/lib/python3.6/site-packages/pkg_resources/__init__.py", line 1049, in best_match
       return self.obtain(req, installer)
     File "/usr/lib/python3.6/site-packages/pkg_resources/__init__.py", line 1061, in obtain
       return installer(requirement)
     File "/usr/lib/python3.6/site-packages/setuptools/dist.py", line 732, in fetch_build_egg
       return fetch_build_egg(self, req)
     File "/usr/lib/python3.6/site-packages/setuptools/installer.py", line 128, in fetch_build_egg
       raise DistutilsError(str(e)) from e
   distutils.errors.DistutilsError: Command '['/usr/bin/python3.6', '-m', 'pip', '--disable-pip-version-check', 'wheel', '--no-deps', '-w', '/tmp/tmpa3b9n_qa', '--quiet', 'cppy>=1.1.0']' returned non-zero exit status 1.
   ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
Error response from daemon: The command '/bin/sh -c pip3 install matplotlib' returned a non-zero code: 1

由于我花了一些时间,并且由于 matplotlib 是用于开发的依赖项,所以我仍然决定将其作为结合@β.εηοιτ.βε[=15 指出的良好实践的答案进行推送=]

正如我在评论中所报告的那样,您缺少很多依赖项来从 pip 安装 matploblib,这将在旅途中构建。

这是一个 Dockerfile,它将在单个映像层中安装 matplotlib,通过在最后一步中删除构建依赖项来保持尽可能薄

FROM openjdk:8-jre-alpine

RUN apk add --no-cache tesseract-ocr python3 py3-numpy && \
    pip3 install --upgrade pip setuptools wheel && \
    apk add --no-cache --virtual .build-deps gcc g++ zlib-dev make python3-dev py-numpy-dev jpeg-dev && \
    pip3 install matplotlib && \
    apk del .build-deps