Docker – Installing Shapely on Alpine Docker

alpinedjangodockerpython

I am trying to move my project to docker and I've been using the alpine variant. This is a django project with postgres database

Here is how i install the dependencies:

...
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 are Shaply's dependency. everything goes fine until i do a try to start the web server since i am testing i'm just using the runserver command in docker-compose and it results in

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 [].

now i am guessing that either i have removed some dependency or i need to install some other libraries. Did some googling and apparently i need to install sudo apt-get install libgeos-c1 but i can not find the alpine/apk equivalent

Best Answer

I struggled with the same issue (not working with Django and PostGres database though).

Finally I managed to tackle this with the solution of Amir. I added the repositories from dl-cdn.alpinelinux.org according to https://github.com/appropriate/docker-postgis/blob/master/Dockerfile.alpine.template. The part that was crucial was running geos-config after the implementation of geos-dev and geos. After this I installed the python modules, including pandas. At the clean up section .build-deps is skipped as said.

This is the part of my Dockerfile that did the trick:

...
RUN apk --update add build-base libxslt-dev

RUN apk add --virtual .build-deps \
        --repository http://dl-cdn.alpinelinux.org/alpine/edge/testing \
        --repository 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); print "so:" $2 }' \
    | xargs -r apk info --installed \
    | sort -u)" && \
    apk add --virtual .rundeps $runDeps

RUN geos-config --cflags

RUN pip install --disable-pip-version-check -r requirements.txt

RUN apk del build-base python3-dev && \
    rm -rf /var/cache/apk/*
...

By the way, I also tried the solution provided on https://github.com/calendar42/docker-python-geos/blob/master/Dockerfile. But this didn't work out for me.

Related Topic