Python 在 Docker Alpine 上安装 numpy
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33421965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Installing numpy on Docker Alpine
提问by TDN169
I'm trying to install numpy in a docker container based on Alpine 3.1. I'm using the following Dockerfile:
我正在尝试在基于 Alpine 3.1 的 docker 容器中安装 numpy。我正在使用以下 Dockerfile:
FROM alpine:3.1
RUN apk add --update make cmake gcc g++ gfortran
RUN apk add --update python py-pip python-dev
RUN pip install cython
RUN pip install numpy
This runs fine until pip install numpy
when I get the following error:
这运行良好,直到pip install numpy
出现以下错误:
error: Command "gcc -fno-strict-aliasing -Os -fomit-frame-pointer -DNDEBUG -Os -fomit-frame-pointer -fPIC -Inumpy/core/include -Ibuild/src.linux-x86_64-2.7/numpy/core/include/numpy -Inumpy/core/src/private -Inumpy/core/src -Inumpy/core -Inumpy/core/src/npymath -Inumpy/core/src/multiarray -Inumpy/core/src/umath -Inumpy/core/src/npysort -I/usr/include/python2.7 -Ibuild/src.linux-x86_64-2.7/numpy/core/src/private -Ibuild/src.linux-x86_64-2.7/numpy/core/src/private -Ibuild/src.linux-x86_64-2.7/numpy/core/src/private -c build/src.linux-x86_64-2.7/numpy/core/src/npymath/ieee754.c -o build/temp.linux-x86_64-2.7/build/src.linux-x86_64-2.7/numpy/core/src/npymath/ieee754.o" failed with exit status 1
easy_install-2.7 numpy
gives the same error.
easy_install-2.7 numpy
给出相同的错误。
Are there any config/installation steps I'm missing?
是否有我遗漏的任何配置/安装步骤?
采纳答案by ziky
If you don't necessary need to install numpy
from pypi
, you could install it from alpine repositories. Package is named py-numpy
and is in testing
repository, see here. Minimal Dockerfile
example that works for me
如果你没有必要需要安装numpy
的pypi
,你可以从高山仓库安装它。包已命名py-numpy
并位于testing
存储库中,请参见此处。Dockerfile
对我有用的最小例子
FROM alpine:3.2
ADD repositories /etc/apk/repositories
RUN apk add --update python python-dev gfortran py-pip build-base py-numpy@community
Content of repositories
file
repositories
文件内容
http://dl-cdn.alpinelinux.org/alpine/v3.2/main
@community http://dl-cdn.alpinelinux.org/alpine/edge/community
回答by James Endicott
I've been having a bit of trouble with this myself and, long story short, I would encourage you to ask if it's really worth the hassle. Numpy is enormous when you start adding things to the stack like pandas, gpus, and scipy so the benefit of building it on alpine is limited, the savings over using Debian, Arch, or even Ubuntu are relatively modest when 500MB of your space is on this library anyway.
我自己在这方面遇到了一些麻烦,长话短说,我鼓励你问问这是否真的值得麻烦。当您开始向堆栈中添加诸如 pandas、gpus 和 scipy 之类的东西时,Numpy 是巨大的,因此在 alpine 上构建它的好处是有限的,当您的空间为 500MB 时,使用 Debian、Arch 甚至 Ubuntu 的节省相对较小反正这个图书馆。
That having been said, I threw together an image that does it. I needed as build-time dependencies musl-dev, linux-headers, and g++. I also wound up needing to add openblas from edge for something later in the stack so it's possible that some dependencies from that are required too. But I believe just adding the three former libraries with
话虽如此,我拼凑了一个图像。我需要 musl-dev、linux-headers 和 g++ 作为构建时依赖项。我还需要从边缘添加 openblas 以用于堆栈中稍后的内容,因此可能也需要一些依赖项。但我相信只需添加三个以前的库
apk --no-cache add musl-dev linux-headers g++
should be sufficient to prevent the gcc error you are getting. You can view the image at https://hub.docker.com/r/o76923/alpine-numpy-stack/
应该足以防止您遇到的 gcc 错误。您可以在https://hub.docker.com/r/o76923/alpine-numpy-stack/查看图像
回答by Gil Margolin
Try this:
尝试这个:
RUN apk --no-cache --update-cache add gcc gfortran python python-dev py-pip build-base wget freetype-dev libpng-dev openblas-dev
RUN ln -s /usr/include/locale.h /usr/include/xlocale.h
RUN pip install pandas
回答by Multihunter
This one is about 311MB according to my docker images
:
根据我的说法,这个大约为 311MB docker images
:
FROM python:3.6-alpine
RUN apk add g++
RUN pip install numpy
(Meanwhile python:3.6
is ~900MB by itself)
(同时python:3.6
本身是~900MB)
Have you tried NOT having gcc installed? It might be conflicting? Not sure. This one worked for me as a minimal numpy installation and wanted to share.
你试过没有安装 gcc 吗?可能是矛盾的?没有把握。这个对我来说是一个最小的 numpy 安装并且想分享。
回答by Faylixe
A package is now available in the Alpine repository: py3-numpy
. But you won't be able to use it straightaway.
现在在 Alpine 存储库中提供了一个包:py3-numpy
. 但是您将无法立即使用它。
py3-numpy
installs libraries into /usr/lib/python3.8/site-packages
directory but the default Python module path does not use it:
py3-numpy
将库安装到/usr/lib/python3.8/site-packages
目录中,但默认的 Python 模块路径不使用它:
$ docker run -it python:3.8-alpine sh
/ # apk add --update --no-cache py3-numpy
/ # python
>>> import numpy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'numpy'
>>> import sys
>>> sys.path
['', '/usr/local/lib/python38.zip', '/usr/local/lib/python3.8', '/usr/local/lib/python3.8/lib-dynload', '/usr/local/lib/python3.8/site-packages']
This can be fixed by setting the $PYTHONPATH
environment variable to the path of the site-packages
in /usr/lib
:
这可以通过将$PYTHONPATH
环境变量设置为site-packages
in的路径来解决/usr/lib
:
FROM python:3.8-alpine
RUN apk add --update --no-cache py3-numpy
ENV PYTHONPATH=/usr/lib/python3.8/site-packages
回答by funnydman
With optimizations such as removing build dependencies after build and removing unneeded tests (they are here because we're building the module, not just installing it):
通过优化,例如在构建后删除构建依赖项和删除不需要的测试(它们在这里是因为我们正在构建模块,而不仅仅是安装它):
FROM frolvlad/alpine-python3
RUN apk add --no-cache \
--virtual=.build-dependencies \
g++ file binutils \
musl-dev python3-dev cython && \
apk add libstdc++ openblas && \
ln -s locale.h /usr/include/xlocale.h && \
pip install numpy && \
rm -r /root/.cache && \
find /usr/lib/python3.*/ -name 'tests' -exec rm -r '{}' + && \
find /usr/lib/python3.*/site-packages/ -name '*.so' -print -exec sh -c 'file "{}" | grep -q "not stripped" && strip -s "{}"' \; && \
rm /usr/include/xlocale.h && \
apk del .build-dependencies
Resulting size ~157MB.
结果大小 ~157MB。