无法使用docker安装Geopandas库

问题描述 投票:0回答:2

我想构建一个带有气流的 Docker 容器。该应用程序需要像 Geopandas 这样的地理空间包。当尝试构建 Docker 映像时,尝试安装 Fiona 时失败,它显示“

FileNotFoundError: [Errno 2] No such file or directory: 'gdal-config': 'gdal-config'

。我不知道如何进一步进行。由于我没有在 prod 环境中安装 conda,所以我需要仅使用 pip 安装 geopanda。

以下是docker文件部分:

COPY requirements.txt .
RUN pip install --user -r requirements.txt

以下是requirements.txt

apache-airflow[crypto,celery,postgres,jdbc,mysql,s3,password]==1.10.12
werkzeug<1.0.0
pytz
pyOpenSSL
ndg-httpsclient
gspread
oauth2client
pyasn1
boto3
airtable
numpy
scipy
slackclient
area
google-api-python-client
sqlalchemy
pandas
celery[redis]==4.1.1
analytics-python
networkx
zenpy==2.0.22
pyarrow
google-auth
six==1.13.0
geopandas

我尝试在requirements.txt中单独安装所需的包以及GDAL,但也因相同的错误而失败。我想运行一个 DAG,它使用在 docker 上运行的 geopandas 库

airflow geopandas gdal docker-build
2个回答
0
投票

将软件包安装到 docker 环境中时,除了可能希望加快构建速度之外,与任何其他本地环境没有什么不同。因此,我将回答这个问题以突出显示更快的选项,但是涉及安装 geopandas 的任何其他问题都与这里相关。

我会仔细阅读 geopandas 安装指南。它包含有关您面临的问题的多个警告。 推荐的安装geopandas的方式是使用conda。如果不手动安装依赖项,就无法使用pip安装geopandas,其中一些依赖项无法使用pip安装。所以你可以这样做,但简单地调用

pip install geopandas
是不行的带你去那里。

我建议使用 miniforge,或者特别是因为你正在构建一个 docker 容器,mambaforge,它的编译速度更快。

mamba
是为并行构建环境而编写的 conda 的一个明显更快的直接替代品,但往往会因更严重的错误消息而更严重地崩溃。在我看来,使用 docker 容器时绝对值得加速,但如果你在调试某些东西时遇到困难,你可以随时退回到
conda
,它是随
mamba
安装的。

不要安装

Anaconda
,其中包括
conda
以及从捆绑到您的
defaults
环境中的
base
通道安装的大量软件包,因为这会导致通道的混合和匹配。一般来说,您应该保持基本环境干净,除了那些明确管理通道本身的包(例如 IDE)之外,没有任何包。相反,通过使用
miniforge
mambaforge
,您将默认使用
conda-forge
通道。

安装 mambaforge 然后创建新的 geopandas 环境:

curl -L -O "https://github.com/conda-forge/miniforge/releases/latest/download/Mambaforge-$(uname)-$(uname -m).sh"
bash Mambaforge-$(uname)-$(uname -m).sh

# install whatever env you'd like here. try to build it in one command
# rather than iteratively installing dependencies
mamba create -n mynewenv -c conda-forge python=3.10 geopandas [other packages]

0
投票

我与同样的问题斗争了两天。我现在找到了一个有效的解决方案(至少对我来说)。

康达尝试

首先,我尝试按照上面的建议使用 conda。事实证明这是行不通的。事实上,你最终不得不弄乱气流图像中预安装的 python 包,这是一场噩梦。

解决方法

一种解决方案是自己安装geopandas依赖项。这并不像其他答案或 GeoPandas 网站上所描述的那样令人畏惧。这是一个扩展了原始气流图像的 Docker 文件,它安装了 geopandas 及其依赖项

# Original airflow image
FROM apache/airflow:2.8.4 

# Install geopandas dependencies using linux package manager
USER root
RUN sudo apt update
RUN sudo apt install -y build-essential
RUN sudo apt install -y gdal-bin
RUN sudo apt install -y libgdal-dev
RUN sudo apt install -y proj-bin
RUN sudo apt install -y geos-bin
RUN export CPLUS_INCLUDE_PATH=/usr/include/gdal
RUN export C_INCLUDE_PATH=/usr/include/gdal

# Install geopandas using pip
USER airflow
RUN pip install geopandas

您可能可以更简洁地编写 docker 文件,但这应该可以完成工作。有关在 ubuntu 上安装 gdal 的更多信息,我发现这很有用:https://mothergeo-py.readthedocs.io/en/latest/development/how-to/gdal-ubuntu-pkg.html.

© www.soinside.com 2019 - 2024. All rights reserved.