如何使用 Linux alpine 3.13 在 Docker 图像上安装 hdf5

How to install hdf5 on Docker image with Linux alpine 3.13

我正在用 python 3.7.10 构建一个 Docker 图像(Linux Alpine v3.13) 但当使用 docker build . 构建映像时,软件包 hdf5 将在安装过程中失败。这是我的 Docker 文件:

FROM python:3.7.10-alpine3.13

RUN mkdir /app
WORKDIR /app
COPY requirements.txt /requirements.txt
ENV CRYPTOGRAPHY_DONT_BUILD_RUST=1
RUN apk add --update --no-cache --virtual .tmp gcc libc-dev linux-headers
RUN apk add --no-cache jpeg-dev zlib-dev mariadb-dev libffi-dev openblas-dev libgfortran lapack-dev build-base openssl-dev
RUN apk add --no-cache hdf5 hdf5-dev
RUN pip install -r /requirements.txt
RUN apk --no-cache del build-base

ENV PYTHONUNBUFFERED 1
COPY . /app/

CMD ["uwsgi", "my_app"]

requirements.txt 文件:

h5py==2.10.0
numpy==1.19.2

我试过使用和不使用 --no-binary 标志,但仍然没有成功。有人能够在此 Alpine 版本上安装该库吗?

错误日志:

    17858 |   __pyx_t_1 = H5Ovisit_by_name(__pyx_v_loc_id, __pyx_v_obj_name, __pyx_v_idx_type, __pyx_v_order, __pyx_v_op, __pyx_v_op_data, __pyx_v_lapl_id); if (unlikely(PyErr_Occurred())) __PYX_ERR(0, 1641, __pyx_L1_error)
          |               ^~~~~~~~~~~~~~~~
    In file included from /usr/include/H5Apublic.h:22,
                     from /usr/include/hdf5.h:23,
                     from /tmp/pip-install-fkianwj6/h5py_dfa9366c1fdb47e98e9e2e8de85ac21e/h5py/api_compat.h:27,
                     from /tmp/pip-install-fkianwj6/h5py_dfa9366c1fdb47e98e9e2e8de85ac21e/h5py/defs.c:654:
    /usr/include/H5Opublic.h:213:15: note: declared here
      213 | H5_DLL herr_t H5Ovisit_by_name3(hid_t loc_id, const char *obj_name,
          |               ^~~~~~~~~~~~~~~~~
    /tmp/pip-install-fkianwj6/h5py_dfa9366c1fdb47e98e9e2e8de85ac21e/h5py/defs.c: In function '__pyx_f_4h5py_4defs_H5Pget_driver_info':
    /tmp/pip-install-fkianwj6/h5py_dfa9366c1fdb47e98e9e2e8de85ac21e/h5py/defs.c:21768:13: warning: assignment discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
    21768 |   __pyx_t_1 = H5Pget_driver_info(__pyx_v_plist_id); if (unlikely(PyErr_Occurred())) __PYX_ERR(0, 2016, __pyx_L1_error)
          |             ^
    In file included from /usr/include/H5public.h:32,
                     from /usr/include/hdf5.h:22,
                     from /tmp/pip-install-fkianwj6/h5py_dfa9366c1fdb47e98e9e2e8de85ac21e/h5py/api_compat.h:27,
                     from /tmp/pip-install-fkianwj6/h5py_dfa9366c1fdb47e98e9e2e8de85ac21e/h5py/defs.c:654:
    /tmp/pip-install-fkianwj6/h5py_dfa9366c1fdb47e98e9e2e8de85ac21e/h5py/defs.c: In function '__pyx_f_4h5py_4defs_H5Sencode':
    /tmp/pip-install-fkianwj6/h5py_dfa9366c1fdb47e98e9e2e8de85ac21e/h5py/defs.c:34528:15: error: too few arguments to function 'H5Sencode2'
    34528 |   __pyx_t_1 = H5Sencode(__pyx_v_obj_id, __pyx_v_buf, __pyx_v_nalloc); if (unlikely(PyErr_Occurred())) __PYX_ERR(0, 3303, __pyx_L1_error)
          |               ^~~~~~~~~
    In file included from /usr/include/hdf5.h:38,
                     from /tmp/pip-install-fkianwj6/h5py_dfa9366c1fdb47e98e9e2e8de85ac21e/h5py/api_compat.h:27,
                     from /tmp/pip-install-fkianwj6/h5py_dfa9366c1fdb47e98e9e2e8de85ac21e/h5py/defs.c:654:
    /usr/include/H5Spublic.h:129:15: note: declared here
      129 | H5_DLL herr_t H5Sencode2(hid_t obj_id, void *buf, size_t *nalloc, hid_t fapl);
          |               ^~~~~~~~~~
    In file included from /usr/local/lib/python3.7/site-packages/numpy/core/include/numpy/ndarrayobject.h:21,
                     from /usr/local/lib/python3.7/site-packages/numpy/core/include/numpy/arrayobject.h:4,
                     from /tmp/pip-install-fkianwj6/h5py_dfa9366c1fdb47e98e9e2e8de85ac21e/h5py/api_compat.h:26,
                     from /tmp/pip-install-fkianwj6/h5py_dfa9366c1fdb47e98e9e2e8de85ac21e/h5py/defs.c:654:
    At top level:
    /usr/local/lib/python3.7/site-packages/numpy/core/include/numpy/__multiarray_api.h:1464:1: warning: '_import_array' defined but not used [-Wunused-function]
     1464 | _import_array(void)
          | ^~~~~~~~~~~~~
    error: command 'gcc' failed with exit status 1
    ----------------------------------------

似乎我使用了错误的旧 h5py 版本 (h5py==2.10.0)。在 requirements.txt.

中更新到 h5py==3.2.1 时,以下设置工作正常
FROM python:3.7.10-alpine3.13

RUN mkdir /app
WORKDIR /app
COPY requirements.txt /requirements.txt
ENV CRYPTOGRAPHY_DONT_BUILD_RUST=1
RUN apk add --update --no-cache --virtual .tmp gcc libc-dev linux-headers
RUN apk add --no-cache jpeg-dev zlib-dev mariadb-dev libffi-dev openblas-dev libgfortran lapack-dev build-base openssl-dev
RUN apk add --no-cache hdf5-dev
RUN pip install -r /requirements.txt
RUN apk --no-cache del build-base

ENV PYTHONUNBUFFERED 1
COPY . /app/

CMD ["uwsgi", "my_app"]

requirements.txt 文件:

h5py==3.2.1
numpy==1.20.1

感谢@jordanvrtanoski 和@valiano 的反馈。