[Fixed]-GeoDjango can't find gdal on docker python alpine based image

9πŸ‘

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]

3πŸ‘

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"]
πŸ‘€lpofredc

0πŸ‘

I faced the same issue while installing my Linux virtualenv, and I managed to get it work using the official documentation:

GDAL is an excellent open source geospatial library that has support for reading most vector and raster spatial data formats. Currently, GeoDjango only supports GDAL’s vector data capabilities [2]. GEOS and PROJ.4 should be installed prior to building GDAL.
First download the latest GDAL release version and untar the archive:

wget https://download.osgeo.org/gdal/X.Y.Z/gdal-X.Y.Z.tar.gz
tar xzf gdal-X.Y.Z.tar.gz
cd gdal-X.Y.Z

Configure, make and install:

./configure
make # Go get some coffee, this takes a while.
sudo make install
cd ..

You can refer to the Django documentation for more details: Django Documentation

πŸ‘€Hubert

Leave a comment