繁体   English   中英

Python GDAL 在 docker 图像上找不到应用程序 GDAL

[英]Python GDAL cannot find app GDAL on docker image

我正在尝试将应用程序构建到 docker 容器中,该容器使用 Python 中的 OSGEO/GDAL 库,这些库是 GDAL 程序的包装器。 GDAL 程序似乎安装正常(至少 Docker 报告=> CACHED [3/9] RUN apk add --no-cache gdal没有我可以看到的步骤中的任何错误)但是,当我到达 pip 的步骤时应该引入 GDAL Python 库,它无法查找不存在的文件,一些初始搜索显示可能意味着它找不到 GDAL 程序。 有谁知道如何解决或解决这个问题?

Collecting GDAL~=3.5.1
#11 15.03   Downloading GDAL-3.5.1.tar.gz (752 kB)
#11 15.16      ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 752.4/752.4 KB 7.3 MB/s eta 0:00:00
#11 15.30   Preparing metadata (setup.py): started
#11 15.71   Preparing metadata (setup.py): finished with status 'error'
#11 15.73   error: subprocess-exited-with-error
#11 15.73   
#11 15.73   × python setup.py egg_info did not run successfully.
#11 15.73   │ exit code: 1
#11 15.73   ╰─> [120 lines of output]
#11 15.73       WARNING: numpy not available!  Array support will not be enabled
#11 15.73       running egg_info
#11 15.73       creating /tmp/pip-pip-egg-info-2jyd4zlx/GDAL.egg-info
#11 15.73       writing /tmp/pip-pip-egg-info-2jyd4zlx/GDAL.egg-info/PKG-INFO
#11 15.73       writing dependency_links to /tmp/pip-pip-egg-info-2jyd4zlx/GDAL.egg-info/dependency_links.txt
#11 15.73       writing requirements to /tmp/pip-pip-egg-info-2jyd4zlx/GDAL.egg-info/requires.txt
#11 15.73       writing top-level names to /tmp/pip-pip-egg-info-2jyd4zlx/GDAL.egg-info/top_level.txt
#11 15.73       writing manifest file '/tmp/pip-pip-egg-info-2jyd4zlx/GDAL.egg-info/SOURCES.txt'
#11 15.73       Traceback (most recent call last):
#11 15.73         File "/tmp/pip-install-tjr9j_9m/gdal_6b994752ac484434b194dfc7ccf64728/setup.py", line 105, in fetch_config
#11 15.73           p = subprocess.Popen([command, args], stdout=subprocess.PIPE)
#11 15.73         File "/usr/local/lib/python3.9/subprocess.py", line 951, in __init__
#11 15.73           self._execute_child(args, executable, preexec_fn, close_fds,
#11 15.73         File "/usr/local/lib/python3.9/subprocess.py", line 1821, in _execute_child
#11 15.73           raise child_exception_type(errno_num, err_msg, err_filename)
#11 15.73       FileNotFoundError: [Errno 2] No such file or directory: '../../apps/gdal-config'

这是我的 dockerfile (恐怕基础图像是不可协商的)

FROM python:3.9.12-alpine3.15

RUN apk add --no-cache gdal

RUN mkdir -p /usr/src/app/file
WORKDIR /usr/src/app
COPY requirements.txt /usr/src/app/
RUN pip install --no-cache-dir -r requirements.txt
COPY src/ /usr/src/app/src/
COPY main.py /usr/src/app/
CMD ["python", "main.py"]

requirements.txt 如下。

requests>= 2.25.0
numpy>=1.23.1
pillow>=9.2.0
GDAL~=3.5.1
DateTime~=4.3
bitstring~=3.1.9
behave~=1.2.6

我不确定是否有办法将 GDAL 安装到特定位置,以便 python 在安装其 GDAL 库时可以找到它,或者我是否需要给 pip 某种提示,或者是否有其他事情正在发生。 如果有人在 docker 容器中使用过这个库,在此先感谢!

问题的主要推动力是缺少 gdal-dev,一个额外的 package 单独安装 python 绑定。 然而,一旦 pip 能够找到 gdal 并开始安装 python 库,这些库是从 C 代码构建的,并且需要在映像中添加一些额外的工具以及指向正确位置的一些环境设置。 这是最终工作的 dockerfile:

FROM python:3.9.12-alpine3.15

RUN apk add --no-cache gcc
RUN apk add --no-cache gdal
RUN apk add --no-cache gdal-dev
RUN apk add --no-cache build-base
RUN apk add --no-cache zlib

RUN export CPLUS_INCLUDE_PATH=/usr/include/gdal
RUN export C_INCLUDE_PATH=/usr/include/gdal
RUN export LDFLAGS="-L/usr/local/opt/zlib/lib"
RUN export CPPFLAGS="-I/usr/local/opt/zlib/include"

RUN mkdir -p /usr/src/app/file
WORKDIR /usr/src/app
COPY requirements.txt /usr/src/app/
RUN pip3 install --no-cache-dir -r requirements.txt
COPY src/ /usr/src/app/src/
COPY main.py /usr/src/app/
CMD ["python", "main.py"]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM