Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GeoDjango can't find gdal on docker python alpine based image

I am creating a geodjango container (based on Python alpine official image) with gdal. When starting the container, I get the following error:

>>> from django.contrib.gis import gdal
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/li...
django.core.exceptions.ImproperlyConfigured: Could not find the GDAL library (tried "gdal", "GDAL", "gdal2.3.0", "gdal2.2.0", "gdal2.1.0", "gdal2.0.0", "gdal1.11.0"). Is GDAL installed? If it is, try setting GDAL_LIBRARY_PATH in your settings.

my image contains following gdal libs:

# find / -name libgdal*
/usr/lib/libgdal.so.20
/usr/lib/libgdal.so.20.5.0
/usr/lib/libgdal.a
/usr/lib/libgdal.so

Adding GDAL_LIBRARY_PATH='usr/lib/libgdal.so.20' to my django settings didn't solve this problem.

My Dockerfile, based on official python3 alpine image.

FROM python:alpine

ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1

WORKDIR /usr/src/dbchiro

COPY requirements.txt /usr/src/dbchiro

# GeoDjango Dependencies

RUN  echo "http://dl-cdn.alpinelinux.org/alpine/edge/testing" > /etc/apk/repositories \
     && echo "http://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories \
     && echo "http://dl-cdn.alpinelinux.org/alpine/edge/main" >> /etc/apk/repositories  \
     && apk add --virtual .build-deps zlib-dev jpeg-dev gdal-dev musl-dev postgresql-dev\
      alpine-sdk libffi-dev jpeg-dev python-dev zlib-dev libffi gcc \ 
     && apk add --no-cache postgresql-libs geos gdal postgresql-client libpq proj nginx \ 
     && python3 -m pip install --upgrade pip --no-cache-dir \
     && python3 -m pip install -r requirements.txt --no-cache-dir \
     && python3 -m pip install gunicorn --no-cache-dir \
     && apk --purge del .build-deps


COPY docker-entrypoint.sh /usr/bin/docker-entrypoint.sh

COPY . /usr/src/dbchiro

VOLUME ["/app","/app/media"]

WORKDIR /app

EXPOSE 80

ENTRYPOINT ["/usr/bin/docker-entrypoint.sh"]

CMD ["/bin/sh"]

docker-entrypoint

#!/bin/sh

export DBHOST=${DBHOST:-db}
...

shopt -s dotglob nullglob
mv /usr/src/dbchiro/* /app/

ls /app

cp -a /app/dbchiro/settings/configuration/config.py.sample /app/dbchiro/settings/configuration/config.py

sed -i "s/dbHost/${DBHOST}/g" /app/dbchiro/settings/configuration/config.py
...

until pg_isready -h $DBHOST -p $DBPORT
do
  echo "Awaiting Database container"
  sleep 1
done
sleep 2

cd /app

export DJANGO_SETTINGS_MODULE=dbchiro.settings.$SETTINGS

python -m manage makemigrations  accounts core blog dicts geodata management sights
python -m manage migrate  
python -m manage collectstatic --noinput 

cp /app/static/img/logo_site.png.sample /app/static/img/logo_site.png

#nginx Conf

cp /app/dbchiro/settings/configuration/nginx_dbchiro.conf.sample /etc/nginx/conf.d/dbchiro.conf
rm /etc/nginx/sites-enabled/*
sed -i "s/pathToApp/app/g" /etc/nginx/conf.d/dbchiro.conf
/etc/init.d/nginx restart

gunicorn -b 127.0.0.1:8000 dbchiro.wsgi

requirements

arrow==0.15.2
backports.csv==1.0.7
colorlog==4.0.2
defusedxml==0.6.0
Django==2.2.5
django-autocomplete-light==3.4.1
django-ckeditor==5.7.1
django-crispy-forms==1.7.2
django-debug-toolbar==2.0
django-extensions==2.2.1
django-filter==2.2.0
django-geojson==2.12.0
django-js-asset==1.2.2
django-leaflet==0.24.0
django-nested-admin==3.2.4
django-tables2==2.1.1
et-xmlfile==1.0.1
jdcal==1.4.1
odfpy==1.4.0
openpyxl==3.0.0
Pillow==6.1.0
psycopg2-binary==2.8.3
python-dateutil==2.8.0
python-monkey-business==1.0.0
pytz==2019.2
PyYAML==5.1.2
six==1.12.0
sqlparse==0.3.0
tablib==0.13.0
xlrd==1.2.0
xlwt==1.3.0

Please some help, full project here https://framagit.org/fred.perso/dbchiroweb/tree/docker

like image 340
lpofredc Avatar asked Oct 15 '19 22:10

lpofredc


2 Answers

I also struggled with this one for a while, the final solution proved quite simple (im using MySql so less dependencies):

Install the dependencies normally in the Dockerfile, e.g:

RUN apk add --no-cache geos gdal 

And then setup their respective variables in the Django settings using glob, e.g:

from glob import glob

GDAL_LIBRARY_PATH=glob('/usr/lib/libgdal.so.*')[0]
GEOS_LIBRARY_PATH=glob('/usr/lib/libgeos_c.so.*')[0]
like image 93
Na'aman Hirschfeld Avatar answered Nov 10 '22 22:11

Na'aman Hirschfeld


Solution was to add binutils to permanently installed packages. This is my final Dockerfile:

FROM python:alpine

ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1

WORKDIR /usr/src/dbchiro

COPY requirements.txt /usr/src/dbchiro

RUN apk add --no-cache \
            --upgrade \
            --repository http://dl-cdn.alpinelinux.org/alpine/edge/main \
        postgresql-client \
        libpq \
        nginx\
    && apk add --no-cache \
               --upgrade \
               --repository http://dl-cdn.alpinelinux.org/alpine/edge/main \
               --virtual .build-deps \
        postgresql-dev \
        zlib-dev jpeg-dev \ 
        alpine-sdk \
    && apk add --no-cache \
               --upgrade \
               --repository http://dl-cdn.alpinelinux.org/alpine/edge/testing \
        geos \
        proj \
        gdal \
        binutils \
    && ln -s /usr/lib/libproj.so.15 /usr/lib/libproj.so \
    && ln -s /usr/lib/libgdal.so.20 /usr/lib/libgdal.so \
    && ln -s /usr/lib/libgeos_c.so.1 /usr/lib/libgeos_c.so \
    && mkdir /var/run/nginx

COPY requirements.txt /usr/src/dbchiro

RUN  python3 -m pip install --upgrade pip --no-cache-dir \
     && python3 -m pip install -r requirements.txt --no-cache-dir \
     && python3 -m pip install gunicorn --no-cache-dir \ 
     && apk --purge del .build-deps

COPY docker-entrypoint.sh /usr/bin/docker-entrypoint.sh

COPY . /usr/src/dbchiro

VOLUME ["/dbchiro"]

WORKDIR /app

EXPOSE 80

ENTRYPOINT ["/usr/bin/docker-entrypoint.sh"]
like image 3
lpofredc Avatar answered Nov 10 '22 23:11

lpofredc