在docker中运行引用了gdal库的Python程序 - docker 容器 gdal

# 前言

需要将一个包含了gdal环境的Python开发环境, 并且部署到docker容器中, 这么通用的一个功能, 为啥网上资源这么少, 又这么麻烦, GDAL在Python中不是用的挺多的嘛 ~ 搞得时候, 一个坑接着一个坑, 爬坑最扰人 ~

1.gdal篇 : gdal镜像 + gdal容器

# 镜像 - 下载 `gdal` 的最新小型 Ubuntu 镜像 sudo docker pull ghcr.io/osgeo/gdal:ubuntu-small-latest # 容器 - 运行 `gdal` 容器: # - `-itd`:交互式运行容器,分配伪终端,并在后台运行。 # - `--name gdal_lx`:为容器指定名称为 `gdal_lx`。 # - `ghcr.io/osgeo/gdal:ubuntu-small-latest`:使用 `gdal` 的最新小型 Ubuntu 镜像。 sudo docker run -itd --name gdal_lx ghcr.io/osgeo/gdal:ubuntu-small-latest

2.gdal & python篇 : 在gdal容器中安装python环境

# 进入容器 `gdal_lx` sudo docker exec -it gdal_lx /bin/bash # 进入容器 `gdal_lx` 并且安装 Python 环境 ( 注意,以下命令是在gdal_lx容器中运行的 ) # 容器 `gdal_lx` 中 - 更新源 apt-get update # 容器 `gdal_lx` 中 - 安装Python3 apt-get install python3 # 容器 `gdal_lx` 中 - 安装pip3 apt-get install pip # 容器 `gdal_lx` 中 - 查看Python版本 python --version # 将当前运行的 `gdal_lx` 容器保存为新镜像: # - `-a "zf"`:指定作者为 "作者名称"。 # - `-m "gdal contain python"`:添加提交描述信息。 # - `gdal_lx`:源容器名。 # - `gdal_python_lx_image:v1`:新镜像的名称和版本号。 sudo docker commit -a "作者名称" -m "gdal contain python" gdal_lx gdal_python_lx_image:v1

整理这篇文章的时候, 我还处于各种尝试, 所以命令没有留下来,

我是发现这种方法行得通的时候, 回过头来整理这篇文档的,

命令我都是查的历史命令来看的, 包括在这个gdal_lx容器中指令命令历史,

我也是再次进入这个容器, 查找的历史命令( 庆幸这个容器还保留着 !)

下面是查找最近1000条不重复的命令

1.查找命令, 并保存到文件commands.txt

history | awk '{ $1=""; print $0 }' | tac | awk '!seen[$0]++' | tac | tail -n 1000 > commands.txt

2.查看具体命令

cat commands.txt

3.根据包含了gdal&python环境的镜像{gdal_python_lx_image:v1}生成服务镜像

# 准备工作 1. 编写好Dockerfile 2. 将Python项目的文件放到这个目录` /home/test/services/cogtif_data_service ` 下( 这个目录下,要有Dockerfile,main.py,,,)
# 执行命令 cd /home/test/services/cogtif_data_service # 根据当前目录下的 Dockerfile 构建一个名为 `cogtif_data_service` 的镜像: # - `-t cogtif_data_service`:指定镜像的名称和标签。 # - `.`:上下文路径为当前目录。 sudo docker build -t cogtif_data_service . # 运行一个基于 `cogtif_data_service` 镜像的容器: # - `-d`:后台运行。 # - `--name cogtif_data_service`:容器命名为 `cogtif_data_service`。 sudo docker run -d --name cogtif_data_service cogtif_data_service # 查看 `cogtif_data_service` 容器的最近 222 行日志。 sudo docker logs --tail 222 cogtif_data_service
# 项目目录大概是这个样子: project_name/ ├── src/ # 主代码目录 │ ├── __init__.py # 包初始化文件 │ ├── main.py # 项目的入口文件 │ ├── module1/ # 模块1 │ │ ├── __init__.py │ │ └── module1_file.py │ └── module2/ # 模块2 │ ├── __init__.py │ └── module2_file.py ├── tests/ # 测试代码 │ ├── __init__.py │ ├── test_module1.py # 测试模块1的代码 │ └── test_module2.py # 测试模块2的代码 ├── requirements.txt # 项目依赖文件 ├── Dockerfile # 生成镜像的时候需要的 ├── .gitignore # Git忽略规则文件 ├── main.py # 项目入口 └── LICENSE # 开源协议(如果需要)
# Dockerfile内容 : FROM gdal_python_lx_image:v1 # 设置工作目录 WORKDIR /app # 将当前项目代码拷贝到工作目录 COPY . /app # 升级 pip RUN apt-get update RUN apt-get install -y pip # 解决一个错误 RUN mv /usr/lib/python3.12/EXTERNALLY-MANAGED /usr/lib/python3.12/EXTERNALLY-MANAGED.bk RUN pip install numpy --ignore-installed numpy # 安装依赖 RUN pip install --no-cache-dir -r requirements.txt # 定义容器启动命令 CMD ["python", "main.py"]

在这一步, 其实遇到的问题最多, 安装的时候, 除了这个gdal和python的问题,

还有安装包依赖的问题, 各种报错, 主要是包的版本问题, 和包中依赖的一些包版本问题,

总结.

总结下思路, 1.先搞一个gdal的镜像, 并且运行起来这个gdal环境的容器, 我们称之为{gdal容器}, 2.在{gdal容器}中安装python开发环境,现在这个{gdal容器}已经超进化到了{gdal,python二合一容器} 3.将{gdal,python二合一容器}提交为一个镜像 {gdal,python二合一镜像}, 在Dockerfile中就使用这个镜像 4.编写的Dockerfile中使用的就是这个{gdal,python二合一镜像},构建一个新的镜像, 并运行为容器
需要要有化整为零的思维,

踩坑-1-

# 去官网上搜索gdal, 会出现一个Star比较多的一个仓库 : "https://hub.docker.com/r/osgeo/gdal/tags" # 选择好了Tag, 然后直接复制上面的命令,比如"sudo docker pull osgeo/gdal:ubuntu-full-3.6.2",执行的时候却报错!错误如下 sudo docker pull osgeo/gdal:ubuntu-full-3.6.2 Error response from daemon: Get "https://registry-1.docker.io/v2/": net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers) # 我尝试更换docker源还是不行,最后使用这个命令才能下载下来 ! sudo docker pull ghcr.io/osgeo/gdal:ubuntu-small-latest 搜了下, 主要区别是镜像来源, osgeo/gdal 是 GDAL 官方提供的镜像( 所以网络不通 ? ) ghcr.io/osgeo/gdal 是存放在 GitHub Container Registry 的镜像(官方也在维护)。( 只要能访问到GitHub,就能docker pull 下来? )

摘抄文档


__EOF__

本文作者国产凌凌漆
本文链接https://www.cnblogs.com/love-zf/p/18689538.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   zh89233  阅读(26)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
历史上的今天:
2021-01-24 . net core sdk 的版本 5.0.101 至少需要 msbuild 的 16.8.0 版本。当前可用的 msbuild 版本为 16.6.0.22303。
点击右上角即可分享
微信分享提示