我正在尝试将我的项目移至 docker,我一直在使用 alpine 版本。这是一个带有 postgres 数据库的 django 项目
以下是我安装依赖项的方法:
...
ADD ./requirements.txt /srv/sites/mysite/requirements.txt
RUN echo "http://mirror.leaseweb.com/alpine/edge/testing" >>
/etc/apk/repositories
RUN apk add --no-cache --virtual .build-deps \
build-base postgresql-dev libffi-dev gcc libc-dev linux-headers bash \
geos geos-dev jpeg-dev zlib-dev \
&& pip install -r requirements.txt \
&& find /usr/local \
\( -type d -a -name test -o -name tests \) \
-o \( -type f -a -name '*.pyc' -o -name '*.pyo' \) \
-exec rm -rf '{}' + \
&& runDeps="$( \
scanelf --needed --nobanner --recursive /usr/local \
| awk '{ gsub(/,/, "\nso:", $2); print "so:" $2 }' \
| sort -u \
| xargs -r apk info --installed \
| sort -u \
)" \
&& apk add --virtual .rundeps $runDeps \
&& apk del .build-deps
...
geos geos-dev
是 Shaply 的依赖项。一切都很顺利,直到我尝试启动 Web 服务器,因为我正在测试,所以我只是使用命令runserver
,docker-compose
结果
web_1_ba4690531efe | Traceback (most recent call last):
web_1_ba4690531efe | File "/srv/sites/mysite/manage.py", line 10, in <module>
web_1_ba4690531efe | execute_from_command_line(sys.argv)
web_1_ba4690531efe | File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
web_1_ba4690531efe | utility.execute()
web_1_ba4690531efe | File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 338, in execute
web_1_ba4690531efe | django.setup()
web_1_ba4690531efe | File "/usr/local/lib/python3.6/site-packages/django/__init__.py", line 27, in setup
web_1_ba4690531efe | apps.populate(settings.INSTALLED_APPS)
web_1_ba4690531efe | File "/usr/local/lib/python3.6/site-packages/django/apps/registry.py", line 108, in populate
web_1_ba4690531efe | app_config.import_models()
web_1_ba4690531efe | File "/usr/local/lib/python3.6/site-packages/django/apps/config.py", line 202, in import_models
web_1_ba4690531efe | self.models_module = import_module(models_module_name)
web_1_ba4690531efe | File "/usr/local/lib/python3.6/importlib/__init__.py", line 126, in import_module
web_1_ba4690531efe | return _bootstrap._gcd_import(name[level:], package, level)
web_1_ba4690531efe | File "<frozen importlib._bootstrap>", line 994, in _gcd_import
web_1_ba4690531efe | File "<frozen importlib._bootstrap>", line 971, in _find_and_load
web_1_ba4690531efe | File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
web_1_ba4690531efe | File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
web_1_ba4690531efe | File "<frozen importlib._bootstrap_external>", line 678, in exec_module
web_1_ba4690531efe | File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
web_1_ba4690531efe | File "/srv/sites/mysite/utils/models.py", line 5, in <module>
web_1_ba4690531efe | from utils.common_models import AbstractDetailedPerson, AbstractTimeStamped
web_1_ba4690531efe | File "/srv/sites/mysite/utils/common_models.py", line 5, in <module>
web_1_ba4690531efe | from utils.functions import national_id_validator
web_1_ba4690531efe | File "/srv/sites/mysite/utils/functions.py", line 16, in <module>
web_1_ba4690531efe | from shapely.geometry import Polygon, Point
web_1_ba4690531efe | File "/usr/local/lib/python3.6/site-packages/shapely/geometry/__init__.py", line 4, in <module>
web_1_ba4690531efe | from .base import CAP_STYLE, JOIN_STYLE
web_1_ba4690531efe | File "/usr/local/lib/python3.6/site-packages/shapely/geometry/base.py", line 17, in <module>
web_1_ba4690531efe | from shapely.coords import CoordinateSequence
web_1_ba4690531efe | File "/usr/local/lib/python3.6/site-packages/shapely/coords.py", line 8, in <module>
web_1_ba4690531efe | from shapely.geos import lgeos
web_1_ba4690531efe | File "/usr/local/lib/python3.6/site-packages/shapely/geos.py", line 76, in <module>
web_1_ba4690531efe | free = load_dll('c').free
web_1_ba4690531efe | File "/usr/local/lib/python3.6/site-packages/shapely/geos.py", line 56, in load_dll
web_1_ba4690531efe | libname, fallbacks or []))
web_1_ba4690531efe | OSError: Could not find lib c or load any of its variants [].
现在我猜要么我已经删除了一些依赖项,要么我需要安装一些其他库。谷歌搜索了一下,显然我需要安装,sudo apt-get install libgeos-c1
但我找不到alpine/apk
等效的
答案1
我遇到了同样的问题(尽管无法使用 Django 和 PostGres 数据库)。
最后我设法用 Amir 的解决方案解决了这个问题。我根据以下内容从 dl-cdn.alpinelinux.org 添加了存储库https://github.com/appropriate/docker-postgis/blob/master/Dockerfile.alpine.template。关键部分是在实现 geos-dev 和 geos 之后运行 geos-config。之后我安装了 python 模块,包括 pandas。在清理部分,.build-deps 被跳过,如前所述。
这是我的 Dockerfile 中实现该技巧的部分:
... 运行 apk --update 添加 build-base libxslt-dev 运行 apk 添加 --virtual .build-deps \ --存储库 http://dl-cdn.alpinelinux.org/alpine/edge/testing \ --存储库 http://dl-cdn.alpinelinux.org/alpine/edge/main \ gcc libc-dev geos-dev geos && \ runDeps="$(scanelf --needed --nobanner --recursive /usr/local \ | awk'{gsub(/,/,“\nso:”,$2);打印“所以:”$2}' \ | xargs -r apk 信息 --已安装 \ | 排序 -u)” && \ apk 添加 --virtual .rundeps $runDeps 运行 geos-config --cflags 运行 pip install --disable-pip-version-check -r requirements.txt 运行 apk del build-base python3-dev && \ rm -rf /var/cache/apk/* ...
顺便说一句,我也尝试了https://github.com/calendar42/docker-python-geos/blob/master/Dockerfile. 但这对我来说没用。
答案2
我遇到了同样的问题,并通过安装 gcc 解决了该问题apk add gcc
。
仔细观察,问题是由这个错误在高山图像中。
一个可能的(相当 hack 的)解决方法(除了安装 gcc,它相当繁重)是修补文件,/sbin/ldconfig
将第一行替换为
#!/bin/sh
if [ "$1" = "-p" ]; then
# Hack to mimic GNU ldconfig s -p option, needed by ctypes, used by shapely
echo " libc.musl-x86_64.so.1 (libc6,x86-64) => /lib/libc.musl-x86_64.so.1"
exit 0
fi
答案3
看起来现在 geos 是 alpine 3.11,正在尝试
FROM lsiobase/alpine:3.11
对我来说似乎有用