web-dev-qa-db-ja.com

docker Alpineにpandasをインストールする

dockerに安定したデータサイエンスパッケージ構成をインストールするのに本当に苦労しています。これは、このような主流の関連ツールを使用すると簡単になります。

以下はDockerfile thatusedを使用して少しハックし、pandasをパッケージコアから削除して個別にインストールし、_pandas<0.21.0_。これは、より高いバージョンがnumpyと競合するためとされています。

_    FROM Alpine:3.6

    ENV PACKAGES="\
    dumb-init \
    musl \
    libc6-compat \
    linux-headers \
    build-base \
    bash \
    git \
    ca-certificates \
    freetype \
    libgfortran \
    libgcc \
    libstdc++ \
    openblas \
    tcl \
    tk \
    libssl1.0 \
    "

ENV PYTHON_PACKAGES="\
    numpy \
    matplotlib \
    scipy \
    scikit-learn \
    nltk \
    " 

RUN apk add --no-cache --virtual build-dependencies python3 \
    && apk add --virtual build-runtime \
    build-base python3-dev openblas-dev freetype-dev pkgconfig gfortran \
    && ln -s /usr/include/locale.h /usr/include/xlocale.h \
    && python3 -m ensurepip \
    && rm -r /usr/lib/python*/ensurepip \
    && pip3 install --upgrade pip setuptools \
    && ln -sf /usr/bin/python3 /usr/bin/python \
    && ln -sf pip3 /usr/bin/pip \
    && rm -r /root/.cache \
    && pip install --no-cache-dir $PYTHON_PACKAGES \
    && pip3 install 'pandas<0.21.0' \    #<---------- PANDAS
    && apk del build-runtime \
    && apk add --no-cache --virtual build-dependencies $PACKAGES \
    && rm -rf /var/cache/apk/*

# set working directory
WORKDIR /usr/src/app

# add and install requirements
COPY ./requirements.txt /usr/src/app/requirements.txt # other than data science packages go here
RUN pip install -r requirements.txt

# add entrypoint.sh
COPY ./entrypoint.sh /usr/src/app/entrypoint.sh

RUN chmod +x /usr/src/app/entrypoint.sh

# add app
COPY . /usr/src/app

# run server
CMD ["/usr/src/app/entrypoint.sh"]
_

上記の設定が機能していました。何が起こるかnowビルドは成功するが、pandasが失敗するat import次のエラーが発生しました:

_ImportError: Missing required dependencies ['numpy']
_

_numpy 1.16.1_がインストールされているため、どのnumpypandasが検索しようとしているかわかりません...

これについて安定した解決策を取得する方法を誰かが知っていますか?

[〜#〜]注[〜#〜]:ターンキーdocker画像からのプルから成るソリューションで、少なくとも上記のパッケージを使用して、上記のDockerfileに、また、大歓迎です。


編集1

データパッケージのインストールを_requirements.txt_に移動すると、コメントのとおり、次のようになります。

requirements.txt

_(...)
numpy==1.16.1 # or numpy==1.16.0
scikit-learn==0.20.2
scipy==1.2.1
nltk==3.4   
pandas==0.24.1 # or pandas== 0.23.4
matplotlib==3.0.2 
(...)
_

およびDockerfile

_# add and install requirements
COPY ./requirements.txt /usr/src/app/requirements.txt
RUN pip install -r requirements.txt
_

pandasで再び壊れ、numpyについて不平を言います。

_Collecting numpy==1.16.1 (from -r requirements.txt (line 61))
  Downloading https://files.pythonhosted.org/packages/2b/26/07472b0de91851b6656cbc86e2f0d5d3a3128e7580f23295ef58b6862d6c/numpy-1.16.1.Zip (5.1MB)
Collecting scikit-learn==0.20.2 (from -r requirements.txt (line 62))
  Downloading https://files.pythonhosted.org/packages/49/0e/8312ac2d7f38537361b943c8cde4b16dadcc9389760bb855323b67bac091/scikit-learn-0.20.2.tar.gz (10.3MB)
Collecting scipy==1.2.1 (from -r requirements.txt (line 63))
  Downloading https://files.pythonhosted.org/packages/a9/b4/5598a706697d1e2929eaf7fe68898ef4bea76e4950b9efbe1ef396b8813a/scipy-1.2.1.tar.gz (23.1MB)
Collecting nltk==3.4 (from -r requirements.txt (line 64))
  Downloading https://files.pythonhosted.org/packages/6f/ed/9c755d357d33bc1931e157f537721efb5b88d2c583fe593cc09603076cc3/nltk-3.4.Zip (1.4MB)
Collecting pandas==0.24.1 (from -r requirements.txt (line 65))
  Downloading https://files.pythonhosted.org/packages/81/fd/b1f17f7dc914047cd1df9d6813b944ee446973baafe8106e4458bfb68884/pandas-0.24.1.tar.gz (11.8MB)
    Complete output from command python setup.py Egg_info:
    Traceback (most recent call last):
      File "/usr/local/lib/python3.7/site-packages/pkg_resources/__init__.py", line 359, in get_provider
        module = sys.modules[moduleOrReq]
    KeyError: 'numpy'

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-install-_e5z6o6_/pandas/setup.py", line 732, in <module>
        ext_modules=maybe_cythonize(extensions, compiler_directives=directives),
      File "/tmp/pip-install-_e5z6o6_/pandas/setup.py", line 475, in maybe_cythonize
        numpy_incl = pkg_resources.resource_filename('numpy', 'core/include')
      File "/usr/local/lib/python3.7/site-packages/pkg_resources/__init__.py", line 1144, in resource_filename
        return get_provider(package_or_requirement).get_resource_filename(
      File "/usr/local/lib/python3.7/site-packages/pkg_resources/__init__.py", line 361, in get_provider
        __import__(moduleOrReq)
    ModuleNotFoundError: No module named 'numpy'

Command "python setup.py Egg_info" failed with error code 1 in /tmp/pip-install-_e5z6o6_/pandas/
_

編集2

これはpandasの未解決の問題のようです。詳細については、以下を参照してください。

pandas-dev github

「残念なことに、pandasインストールされた(Dockerコンテナ内にある))新しい環境を設定するには、requirements.txtファイルでは不十分です。」.

_  **ImportError**:

  IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

  Importing the multiarray numpy extension module failed.  Most
  likely you are trying to import a failed build of numpy.
  Here is how to proceed:
  - If you're working with a numpy git repository, try `git clean -xdf`
    (removes all files not under version control) and rebuild numpy.
  - If you are simply trying to use the numpy version that you have installed:
    your installation is broken - please reinstall numpy.
  - If you have already reinstalled and that did not fix the problem, then:
    1. Check that you are using the Python you expect (you're using /usr/local/bin/python),
       and that you have no directories in your PATH or PYTHONPATH that can
       interfere with the Python and numpy versions you're trying to use.
    2. If (1) looks fine, you can open a new issue at
       https://github.com/numpy/numpy/issues.  Please include details on:
       - how you installed Python
       - how you installed numpy
       - your operating system
       - whether or not you have multiple versions of Python installed
       - if you built from source, your compiler versions and ideally a build log
_

編集3

requirements.txt ---> https://Pastebin.com/0icnx0i


編集4

1/12/20現在、受け入れられたソリューションは機能しなくなりました。ビルドブレークは、pandasではなくscipyでではなくnumpyの後で、_scipy's_ホイールのビルド中に発生します。これはログです:

_  ----------------------------------------
  ERROR: Failed building wheel for scipy
  Running setup.py clean for scipy
  ERROR: Command errored out with exit status 1:
   command: /usr/bin/python3.6 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-s6nahssd/scipy/setup.py'"'"'; __file__='"'"'/tmp/pip-install-s6nahssd/scipy/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' clean --all
       cwd: /tmp/pip-install-s6nahssd/scipy
  Complete output (9 lines):

  `setup.py clean` is not supported, use one of the following instead:

    - `git clean -xdf` (cleans all files)
    - `git clean -Xdf` (cleans all versioned files, doesn't touch
                        files that aren't checked into the git repo)

  Add `--force` to your command to use it anyway if you must (unsupported).

  ----------------------------------------
  ERROR: Failed cleaning build dir for scipy
Successfully built numpy
Failed to build scipy
ERROR: Could not build wheels for scipy which use PEP 517 and cannot be installed directly
_

エラーから、構築プロセスは_python3.6_を使用しているようですが、私は_FROM Alpine:3.7_を使用しています。

ここに完全なログ-> https://Pastebin.com/Tw4ubxSA

そして、これが現在のDockerfileです。

https://Pastebin.com/3SftEufx

23
8-Bit Borges

Alpine 3.6に拘束されていない場合は、Alpine 3.7(またはそれ以降)を使用する必要があります。

Alpine 3.6では、matplotlibのインストールが次の理由で失敗しました:

Collecting matplotlib
  Downloading https://files.pythonhosted.org/packages/26/04/8b381d5b166508cc258632b225adbafec49bbe69aa9a4fa1f1b461428313/matplotlib-3.0.3.tar.gz (36.6MB)
    Complete output from command python setup.py Egg_info:
    Download error on https://pypi.org/simple/numpy/: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:833) -- Some packages may not be found!
    Couldn't find index page for 'numpy' (maybe misspelled?)
    Download error on https://pypi.org/simple/: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:833) -- Some packages may not be found!
    No local packages or working download links found for numpy>=1.10.0

ただし、Alpine 3.7では機能しました。これはnumpyのバージョン管理の問題が原因である可能性があります( ここ を参照)。しかし、確実に特定することはできません。その問題を過ぎると、パッケージは正常にビルドおよびインストールされました-約30分(Alpineのmusl-libcはPythonのWheelsフォーマットと互換性がないため、pipでインストールされるすべてのパッケージはソースからビルドする必要があります)。

重要な変更が1つ必要なことに注意してください。build-runtimeの後にapk del build-runtime仮想パッケージ(pip install)のみを削除する必要があります。また、該当する場合は、numpy 1.16.1を出荷バージョンである1.16.2に置き換えることができます(そうしないと、1.16.2がアンインストールされ、1.16.1がソースからビルドされ、さらにビルドが増加します時間)-しかし、私はこれを試していません。

参考までに、少し変更した Dockerfile およびdocker build output を以下に示します。

注:

通常、画像サイズを最小化するためのベースとしてAlpineが選択されます(Alpineもその他の点では非常に洗練されていますが、glibc/muslにより本土のLinuxアプリとの互換性の問題があります)。 Pythonパッケージをソースからビルドする必要があります。非常に肥大化したイメージが得られるため、クリーンアップの前に900MB。これにはビルドにも時間がかかります。すべての中間コンパイルアーティファクト、ビルド依存関係など。

ソースからビルドせずに、アルパインでの作業に必要なPythonパッケージバージョンを取得できない場合は、debian-slim、またはubuntu

編集:

要件が追加された「編集3」に続いて、ここで更新されます Dockerfile およびDocker ビルド出力 。ビルドの依存関係を満たすために、次のパッケージが追加されました。

postgresql-dev libffi-dev libressl-dev libxml2 libxml2-dev libxslt libxslt-dev libjpeg-turbo-dev zlib-dev

特定のヘッダーのためにビルドに失敗したパッケージについては、Alpineのパッケージコンテンツ検索を使用して、不足しているパッケージを見つけました。特にcffiの場合、ffi.hヘッダーがありません。これにはlibffi-devパッケージが必要です: https://pkgs.alpinelinux.org/contents?file=ffi.h&path =&name =&branch = v3.7

または、パッケージビルドの失敗があまり明確でない場合は、特定のパッケージのインストール手順を参照できます(例: Pillow )。

新しい画像のサイズは、圧縮前の1.04GBです。少し削減するために、Pythonおよびpipキャッシュを削除できます。

RUN apk del build-runtime && \
    find -type d -name __pycache__ -Prune -exec rm -rf {} \; && \
    rm -rf ~/.cache/pip

docker build --squashを使用すると、これにより画像サイズが661MBまで下がります。

14
valiano

これをrequirements.txtファイルに追加してみてください。

numpy==1.16.0
pandas==0.23.4

私は昨日から同じエラーに直面しており、この変更で解決しました。

4
Ram Krishnan

Numpy/pandasのインストールを検索するとポップアップする最初の回答がアルパインで失敗したため、これは完全に関連しているとは限りません。この回答を追加します。

次の修正は私のために働きました(しかし、パンダ/ numpyをインストールするのにもっと時間がかかります)

apk update
apk --no-cache add curl gcc g++
ln -s /usr/include/locale.h /usr/include/xlocale.h

今パンダ/ numpyをインストールしてみてください

0
codeslord

インストールに時間がかかるのはなぜですかPandas に関連しています。

python 3の場合、ナットとボルトを知らずに安定したソリューションを取得することを目的とする場合は、以下を構築できます(私のコピーをコピーして貼り付けます https:// stackoverflow.com/a/50443531/1021819

FROM python:3.7-Alpine
RUN echo "@testing http://dl-cdn.alpinelinux.org/Alpine/Edge/testing" >> /etc/apk/repositories
RUN apk add --update --no-cache py3-numpy py3-pandas@testing

あなたの目標が安定したビルドを達成するための方法を理解することである場合、そこでの議論と関連する画像mightあまりにも助けて...

0
jtlz2