题 docker,alpine和matplotlib


我正在尝试使用alpine缩小我们的一个docker图像:

FROM python:2.7-alpine

但是,我在安装matplotlib.h时遇到麻烦:

EOF
warning: no previously-included files matching '*.pyo' found anywhere in distribution
warning: no previously-included files matching '*.pyd' found anywhere in distribution
numpy/core/src/multiarray/numpyos.c:18:21: fatal error: xlocale.h: No such file or directory
compilation terminated.
numpy/core/src/multiarray/numpyos.c:18:21: fatal error: xlocale.h: No such file or directory
compilation terminated.
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/tmp/pip-build-kCID_8/matplotlib/setup.py", line 383, in <module>
    **extra_args
  File "/usr/local/lib/python2.7/distutils/core.py", line 111, in setup
    _setup_distribution = dist = klass(attrs)
  File "/usr/local/lib/python2.7/site-packages/setuptools/dist.py", line 269, in __init__
    self.fetch_build_eggs(attrs['setup_requires'])
  File "/usr/local/lib/python2.7/site-packages/setuptools/dist.py", line 313, in fetch_build_eggs
    replace_conflicting=True,
  File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 826, in resolve
    dist = best[req.key] = env.best_match(req, ws, installer)
  File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 1071, in best_match
    return self.obtain(req, installer)
  File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 1083, in obtain
    return installer(requirement)
  File "/usr/local/lib/python2.7/site-packages/setuptools/dist.py", line 380, in fetch_build_egg
    return cmd.easy_install(req)
  File "/usr/local/lib/python2.7/site-packages/setuptools/command/easy_install.py", line 640, in easy_install
    return self.install_item(spec, dist.location, tmpdir, deps)
  File "/usr/local/lib/python2.7/site-packages/setuptools/command/easy_install.py", line 670, in install_item
    dists = self.install_eggs(spec, download, tmpdir)
  File "/usr/local/lib/python2.7/site-packages/setuptools/command/easy_install.py", line 850, in install_eggs
    return self.build_and_install(setup_script, setup_base)
  File "/usr/local/lib/python2.7/site-packages/setuptools/command/easy_install.py", line 1078, in build_and_install
    self.run_setup(setup_script, setup_base, args)
  File "/usr/local/lib/python2.7/site-packages/setuptools/command/easy_install.py", line 1066, in run_setup
    raise DistutilsError("Setup script exited with %s" % (v.args[0],))
distutils.errors.DistutilsError: Setup script exited with error: Command "gcc -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -DHAVE_NPY_CONFIG_H=1 -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE=1 -D_LARGEFILE64_SOURCE=1 -DHAVE_CBLAS -Ibuild/src.linux-x86_64-2.7/numpy/core/src/private -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/local/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 numpy/core/src/multiarray/numpyos.c -o build/temp.linux-x86_64-2.7/numpy/core/src/multiarray/numpyos.o" failed with exit status 1

我意识到这可能是因为 musl

以下是我的相关内容 Dockerfile

# xmlsec is only available in the testing repo
RUN apk add --no-cache --virtual .build-deps \
  xmlsec \
  xmlsec-dev \
  py-numpy \
  py-numpy-dev \
  --repository http://dl-3.alpinelinux.org/alpine/edge/testing/ \
  --allow-untrusted

RUN apk add --no-cache --virtual .build-deps \
  build-base \
  postgresql-dev \
  libffi-dev \
  nodejs \
  swig \
  git \
  libpng-dev \
  freetype-dev \
  libxml2-dev \
  libxslt-dev \
  python-dev \
  musl-dev \
  tk-dev \
  gfortran \
  py-setuptools \
  netcat-openbsd

RUN pip install -r requirements.txt

这是我的要求.txt:

setuptools==19.7
alabaster==0.7.4
amqp==1.4.6
anyjson==0.3.3
authy==2.1.2
awscli==1.9.12
Babel==1.3
backports.ssl-match-hostname==3.4.0.2
blessings==1.6
boto==2.9.9
certifi==2015.4.28
click==4.0
cryptography==1.0.1
Django==1.8.2
django-oauth-toolkit==0.8.1
django-appconf==1.0.1
django-compressor==1.5
django-cors-headers==1.1.0
django-coverage==1.2.4
django-crispy-forms==1.5.2
django-crontab==0.6.0
django-debug-panel==0.8.1
django-debug-toolbar==1.3.0
django-easy-pdf==0.1.0
django-email-extras==0.3.1
django-filter==0.10.0
django-forms-builder==0.11.1
django-libsass==0.3
django-markdown-deux==1.0.5
django-model-utils==2.2
django-nose==1.4
django-pgjson==0.3.1
django-phonenumber-field==0.7.2
django-pickling==0.1
django-ratelimit==0.6.0
django-redis==4.0.0
djangorestframework==3.3.1
djangorestframework-csv==1.3.4
django-rq==0.7.0
django-s3-cache==1.4.2
django-sphinx==2.2.4
django-storages==1.1.8
django-templatetag-handlebars==1.3.0
django-timedeltafield==0.7.10
django-twilio==0.8.0
docutils==0.12
ecdsa==0.11
extras==0.0.3
factory-boy==2.5.2
fake-factory==0.5.1
flex==4.3.0
functools32==3.2.3-1
gdata==2.0.18
gnureadline==6.3.3
greenlet==0.4.7
gunicorn==19.3.0
html5lib==0.999
httplib2==0.9.1
httpretty==0.8.10
inflection==0.3.1
ipdb==0.8
ipython==3.1.0
Jinja2==2.7.3
jsonschema==2.5.1
libsass==0.8.2
linecache2==1.0.0
lockfile==0.9.1
lxml==3.4.4
matplotlib==1.5.0
mimeparse==0.1.3
mistune==0.6
mock==1.3.0
newrelic==2.50.0.39
nose==1.3.6
nose-progressive==1.5.1
numpy==1.9.2
oauth2client==1.4.11
oauthlib==0.7.2
pandas==0.16.1
paramiko==1.15.2
pbr==1.0.1
pep8==1.6.2
phonenumbers==7.0.9
Pillow==2.8.1
pipdeptree==0.4.2
progressbar33==2.4
psycopg2==2.5.5
ptyprocess==0.5
pyasn1==0.1.7
pyasn1-modules==0.0.5
pycrypto==2.6.1
Pygments==2.0.2
PyJWT==1.3.0
pyOpenSSL==0.13
PyPDF2==1.24
python-daemon==1.6
python-dateutil==2.4.2
python-gnupg==0.3.7
python-logstash==0.4.6
python-memcached==1.54
python-mimeparse==0.1.4
python-openid==2.2.5
python-saml==2.1.3
python-social-auth==0.2.12
pytz==2015.4
PyYAML==3.11
pyzmq==14.6.0
raven==5.5.0
redis==2.10.3
reportlab==3.1.44
requests==2.7.0
requests-oauthlib==0.5.0
rq==0.5.2
rq-scheduler==0.5.1
rsa==3.1.4
simplejson==3.7.2
six==1.10.0
soaplib==2.0.0-beta2
sorl-thumbnail==12.2
Sphinx==1.3.1
sphinx-me==0.3
sqlparse==0.1.15
ssh==1.8.0
stripe==1.27.1
suds==0.4
terminado==0.5
testtools==1.8.0
traceback2==1.4.0
Unidecode==0.4.17
unittest2==1.0.1
uritemplate==0.6
vobject==0.6.6
Werkzeug==0.9.6
whitenoise==1.0.6
xhtml2pdf==0.0.6
django-absoluteuri==1.1.0

要安装它我需要做什么?


11
2018-04-18 16:01






答案:


我通过添加:

RUN ln -s /usr/include/locale.h /usr/include/xlocale.h

一切都可以正常安装。但我不确定这是好习惯。


12
2017-07-25 15:12



我知道你来自哪里 - 但我觉得能够在我的Dockerfile中编写这个代码更好(在评论中有一个适当的链接回到这个确切的帖子)。 Docker,尽管受到了批评,但令人印象深刻,部分原因是因为这样的事情。 - lol
这也适用于Cygwin。 Numpy只是尝试加载xlocale.h(如果可用)并且由于某种原因它认为它在所有地方的Cygwin中都可用,但它不依赖于xlocale.h的特殊功能。 - CMCDragonkai
提供什么 /usr/include/locale.h ?我正在使用高山图像并已安装 make python gcc libffi-dev linux-headers libgcc libstdc++ 但是首先需要的文件不在那里链接 - Michael Mügge
@MichaelMügge那就是 musl-dev 包 - Céline Aussourd


您可能需要使用使用的高山Linux docker镜像 glibc 并替换 musl-dev 同 glibc-headers,因为缺少的头文件被捆绑在一起 glibc-headers

当您寻找时,Docker Hub中的所有顶级结果都会出现 glibc 启用alpine linux docker镜像 使用 相同的来源 对于 glibc APK包。

该构建不包括标题,但可以通过包含另一个包含丢失文件的子包来调整以实现此目的,这是(未经测试的):

headers() {
  mkdir -p "$subpkgdir"/usr/glibc-compat
  cp -a "$srcdir"/usr/glibc-compat/include "$subpkgdir"/usr/glibc-compat
}

在里面 APKBUILD 文件。

研究过这个之后,我质疑在最终容器中使用标题的有用性。 在我看来,如果可能的话,让中间容器构建需要头的依赖项,并在容器中安装生成的包,会更有用。


2
2018-04-19 20:08



嗯,这对我不起作用。我得到了同样的错误 xlocale.h。我看到它安装了 musl-dev 无论如何,其中一个包似乎需要它。 libc-dev 据我所知,也安装了。 - synic
你能分享一下吗? requirements.txt (或触发实际需要的构建的相关部分 matplotlib.h),在当地重现? - dawud
好的,我把它添加到OP - synic
经过一些研究,它需要一个非常重要的时间来提供一个有效的解决方案,我试图概述要求以及为什么头文件不存在。我留给你填补空白。但希望它有用。 - dawud


这些依赖项对我有用: docker run -c 'apk update && apk add g++ make subversion gcc gfortran ca-certificates python3-dev libpng-dev freetype-dev python3; ln -s /usr/include/locale.h /usr/include/xlocale.h; python3 -m pip install matplotlib'

更换 python3 同 python 如果你想2.7

(除了@ Celine的回答)


1
2018-06-06 22:03