编译带有python/java绑定的gdal3.2.2 docker镜像


在centos中安装gdal3.x是比较麻烦的,所以可以准备一个编译镜像,将gdal在镜像内部编译,每次打其他包的时候从中复制编译好的gdal即可.

编译镜像:

FROM adoptopenjdk/openjdk8:x86_64-centos-jdk8u312-b07

WORKDIR /src

# 下载源码
RUN curl https://www.sqlite.org/2021/sqlite-autoconf-3350400.tar.gz -o sqlite.tar.gz && \
    tar -xzf sqlite.tar.gz && rm -rf sqlite.tar.gz && \
    curl https://download.osgeo.org/proj/proj-8.0.0.tar.gz -o proj.tar.gz && \
    tar -xzf proj.tar.gz && rm -rf proj.tar.gz && \
    curl -L https://github.com/OSGeo/gdal/releases/download/v3.2.2/gdal-3.2.2.tar.gz -o gdal.tar.gz && \
    tar -xzf gdal.tar.gz && rm -rf gdal.tar.gz

# 安装编译依赖
RUN yum -y install gcc gcc-c++ kernel-devel swig ant \
    libcurl-devel flex bison autoconf automake \
    bzip-devel zlib-devel ncurses-devel libjpeg-devel \
    libpng-devel libtiff-devel freetype-devel pam-devel \
    python3 python-devel python3-devel openssl-devel libtool \
    libxml2-devel gettext-devel pcre-devel && yum clean all
    
RUN cd sqlite-autoconf-3350400 && \
    # 编译sqlite
    CFLAGS="-DSQLITE_ENABLE_COLUMN_METADATA=1" \
    ./configure --prefix=/usr/local && \
    make -j$(nproc) && make install DESTDIR="/build" && \
    export SQLITE3_CFLAGS="-I/build/usr/local/include" && \
    export SQLITE3_LIBS="-L/build/usr/local/lib -lsqlite3" && \
    export PATH=/build/usr/local/bin/:$PATH && \
    cd ../proj-8.0.0 && \
    # 编译proj
    CFLAGS='-DPROJ_RENAME_SYMBOLS -O2' \
    CXXFLAGS='-DPROJ_RENAME_SYMBOLS -DPROJ_INTERNAL_CPP_NAMESPACE -O2' \
    ./configure --prefix=/usr/local --disable-static && \
    make -j$(nproc) && make install DESTDIR="/build" && \
    cd ../gdal-3.2.2 && \
    # 编译gdal
    ./configure --prefix=/usr \
    --without-libtool \
    --with-hide-internal-symbols \
    --with-jpeg12 \
    --with-python=/usr/bin/python3 \
    --with-webp \
    --with-proj=/build/usr/local \
    --with-libtiff=internal \
    --with-rename-internal-libtiff-symbols \
    --with-geotiff=internal \
    --with-rename-internal-libgeotiff-symbols \
    --with-java=/opt/java/openjdk \
    --with-jvm-lib=/opt/java/openjdk/jre/lib/amd64/server \
    --with-jvm-lib-add-rpath && \
    make -j$(nproc) && \
    make install DESTDIR="/build" && \
    # 编译GDAL for java
    cd swig/java && \
    make -j$(nproc) && \
    make install DESTDIR="/build"

# 安装python库
RUN pip3 install --no-cache-dir -i https://pypi.tuna.tsinghua.edu.cn/simple cython && \
    export PROJ_DIR=/build/usr/local && \
    pip3 install --no-cache-dir -i https://pypi.tuna.tsinghua.edu.cn/simple pyproj==2.2.0 && \
    pip3 uninstall -y cython 

RUN PROJ_SO=$(readlink /build/usr/local/lib/libproj.so | sed "s/libproj\.so\.//") \
    && PROJ_SO_FIRST=$(echo $PROJ_SO | awk 'BEGIN {FS="."} {print $1}') \
    && mv /build/usr/local/lib/libproj.so.${PROJ_SO} /build/usr/local/lib/libinternalproj.so.${PROJ_SO} \
    && ln -s libinternalproj.so.${PROJ_SO} /build/usr/local/lib/libinternalproj.so.${PROJ_SO_FIRST} \
    && ln -s libinternalproj.so.${PROJ_SO} /build/usr/local/lib/libinternalproj.so \
    && rm -rf /build/usr/local/lib/libproj.*  \
    && ln -s libinternalproj.so.${PROJ_SO} /build/usr/local/lib/libproj.so.${PROJ_SO_FIRST} \
    && strip -s /build/usr/local/lib/libinternalproj.so.${PROJ_SO} \
    && for i in /build/usr/local/bin/*; do strip -s $i 2>/dev/null || /bin/true; done \
    && mkdir -p /build_gdal_python/usr/lib \
    && mkdir -p /build_gdal_python/usr/bin \
    && mkdir -p /build_gdal_version_changing/usr/include \
    && mv /build/usr/lib64/python*          /build_gdal_python/usr/lib \
    && rsync -av /usr/local/lib64/python*   /build_gdal_python/usr/lib \
    && mv /build/usr/lib                    /build_gdal_version_changing/usr \
    && mv /build/usr/include/gdal_version.h /build_gdal_version_changing/usr/include \
    && mv /build/usr/bin/*.py               /build_gdal_python/usr/bin \
    && mv /build/usr/bin                    /build_gdal_version_changing/usr \
    && for i in /build_gdal_version_changing/usr/lib/*; do strip -s $i 2>/dev/null || /bin/true; done \
    && for i in /build_gdal_python/usr/lib/python3.6/site-packages/osgeo/*.so; do strip -s $i 2>/dev/null || /bin/true; done \
    && for i in /build_gdal_version_changing/usr/bin/*; do strip -s $i 2>/dev/null || /bin/true; done

使用镜像

FROM adoptopenjdk/openjdk8:x86_64-centos-jdk8u312-b07

COPY --from=gdal_centos  /build/usr/local/share/proj/ /usr/local/share/proj/
COPY --from=gdal_centos  /build/usr/local/include/ /usr/local/include/
COPY --from=gdal_centos  /build/usr/local/bin/ /usr/local/bin/
COPY --from=gdal_centos  /build/usr/local/lib/ /usr/local/lib/

COPY --from=gdal_centos  /build/usr/share/java /usr/share/java
COPY --from=gdal_centos  /build/usr/share/gdal/ /usr/share/gdal/
COPY --from=gdal_centos  /build/usr/include/ /usr/include/
COPY --from=gdal_centos  /build_gdal_python/usr/ /usr/
COPY --from=gdal_centos  /build_gdal_version_changing/usr/ /usr/

ENV LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib:/usr/lib

RUN ln -s /usr/lib/libgdal.so.3 /usr/lib/libgdal.so.26 && \
    ln -s /usr/lib/libgdal.so.3 /usr/lib/libgdal.so.27 && \
    ln -s /usr/lib/libgdal.so.3 /usr/lib/libgdal.so.28 && \
    yum -y install libjpeg-devel libtiff-devel