我正在尝试在 Docker 中设置 GDAL 以支持使用 Flask 的 Python 后端 Web 服务器。运行 docker compose up -d 后,我收到以下错误:
Could not find gdal-config. Make sure you have installed the GDAL native library and development headers.
我尝试了有关为 Django 设置 Docker 和 GDAL 的类似问题的解决方案:How to add GDAL in docker to run with an alpine Docker image 但这并没有解决错误:
RUN apk --no-cache add ca-certificates tzdata
RUN apk add --no-cache –repository
http://dl-cdn.alpinelinux.org/alpine/edge/testing gdal
我的 Dockerfile:
FROM python:3.10-alpine
COPY requirements.txt /
RUN pip install -r requirements.txt
COPY app /app/
ENV FLASK_APP=/app
ENV FLASK_RUN_HOST=0.0.0.0
ENV FLASK_DEBUG=0
CMD ["gunicorn", "-b", "0.0.0.0:1111", "app:app"]
我的docker-compose:
services:
backend:
build:
context: ./backend
dockerfile: Dockerfile
ports:
- '1111:1111'
您可以通过 apk 安装 gdal 和 gdal-dev 软件包并定义必要的路径,以便 python 库可以找到它们。
FROM python:3.12-alpine
RUN apk upgrade --update
RUN apk add --no-cache build-base gcc gdal gdal-dev 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"
WORKDIR /app
COPY . /app
RUN pip install --no-cache-dir -r requirements.txt
# ...
由于alpine包源提供了gdal 3.9.1版本,所以需要使用python下的兼容版本。所以这条线是在要求范围内的。
gdal==3.9.1
# ...