操作日志
sh
# docker build -t my-python38-app:v1.0 .
Sending build context to Docker daemon 7.168kB
Step 1/7 : FROM docker.1ms.run/library/python:3.8.0-slim
---> 577b86e4ee11
Step 2/7 : WORKDIR /app
---> Running in 0de590086b99
Removing intermediate container 0de590086b99
---> 0f580a03d777
Step 3/7 : COPY requirements.txt .
---> b7ec20df18c6
Step 4/7 : RUN pip install --no-cache-dir -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
---> Running in e39c16176a62
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting dmPython==2.5.30
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/03/a4/c216981ea047e86a297ce84fb318db55c96c60c991b82d4a5f683343a203/dmpython-2.5.30-cp38-cp38-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (10.8MB)
Installing collected packages: dmPython
Successfully installed dmPython-2.5.30
WARNING: You are using pip version 19.3.1; however, version 25.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Removing intermediate container e39c16176a62
---> 701cbb6a6631
Step 5/7 : COPY . .
---> 43ff322f5cd9
Step 6/7 : ENV LD_LIBRARY_PATH=/usr/local/lib/python3.8/site-packages/dmpython.libs:/usr/local/lib/python3.8/site-packages/dmssl/
---> Running in 4ad56431a8cd
Removing intermediate container 4ad56431a8cd
---> bcc24c66e5a9
Step 7/7 : CMD ["python", "app.py"]
---> Running in bab8c3659b84
Removing intermediate container bab8c3659b84
---> 3829e97f0954
Successfully built 3829e97f0954
Successfully tagged my-python38-app:v1.0
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
my-python38-app v1.0 3829e97f0954 22 seconds ago 227MB
registry.cn-hangzhou.aliyuncs.com/lhrbest/oracle23ai 1.0 9e6d2a8997be 21 months ago 15.2GB
swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/mysql 8.0 1eba4c9bcaa8 21 months ago 567MB
prom/pushgateway latest 75cb855e9cef 22 months ago 21.5MB
grafana/grafana-enterprise 10.3.3-ubuntu 75bec58dc24e 24 months ago 488MB
mcr.microsoft.com/mssql/server 2022-latest 683d523cd395 2 years ago 2.9GB
postgres 12.10 a2ef7cfb6b9c 3 years ago 373MB
ibmcom/db2 11.5.6.0 a6a5ee354fb1 4 years ago 2.95GB
portainer/portainer latest 580c0e4e98b0 4 years ago 79.1MB
swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/postgres 12.2 0f10374e5170 5 years ago 314MB
docker.1ms.run/library/python 3.8.0-slim 577b86e4ee11 6 years ago 193MB
swr.ap-southeast-1.myhuaweicloud.com/library/mysql 5.7 f6509bac4980 6 years ago 373MB
# docker run 3829e97f0954
基于官方Python 3.8镜像的自定义镜像运行成功!
Python版本:3.8.x
[(140553249777800, 880, 'select * from v$sessions where sess_id=sessid', 'ACTIVE', 64, 1, 7, 'SYSDBA', 'SYSDBA', 182731, datetime.datetime(2026, 1, 30, 17, 9, 6), 'SQL3', '+00:00', 'N', 'N', 'N', 'Y', 'N', 'Y', 'Y', 'N', 'N', 1, '934c02538989', 'python3.8', '::ffff:xxxxx:38958', 'Linux', 'HOMOGENEOUS', 140553249710120, 'RUNNING', 'RECEIVE', datetime.datetime(2026, 1, 30, 17, 9, 6), datetime.datetime(2026, 1, 30, 17, 9, 6), 'N', 6814, 1, 0, 65535, None, 0, None, None, None, '8.1.4.170', 1027, -1, '', '', '', '')]
# ls
app.py Dockerfile requirements.txt
# cat app.py
# -*- coding: UTF-8 -*-
import sys
import dmPython as dm
import os
def test_dmsession():
try:
conn = dm.connect(
server='ip',
port=端口,
user='用户名',
password='密码'
)
# 创建游标
cursor = conn.cursor()
# 显式准备 SQL 语句
cursor.prepare("select * from v$sessions where sess_id=sessid")
# 绑定参数并执行查询
cursor.execute(None)
# 获取结果
results = cursor.fetchall()
print(results)
except Exception as e:
print(f"Error: {e}")
finally:
# 关闭游标和连接
cursor.close()
conn.close()
if __name__ == "__main__":
print("基于官方Python 3.8镜像的自定义镜像运行成功!")
print("Python版本:3.8.x")
test_dmsession()
sys.exit()
# cat Dockerfile
# 1. 指定基础镜像:Docker官方Python 3.8 轻量版(slim版,去除冗余组件,体积更小)
# 生产/开发均推荐slim,完整版用 python:3.8(包含更多系统工具,体积大)
FROM docker.1ms.run/library/python:3.8.0-slim
# 2. 设置工作目录(容器内的目录,后续所有操作均在此目录执行,避免文件混乱)
# 相当于容器内执行 `mkdir /app && cd /app`,不存在则自动创建
WORKDIR /app
# 3. 复制Python依赖清单到容器工作目录
# 【关键优化】先复制requirements.txt,再安装依赖:利用Docker层缓存
# 若仅修改代码,依赖未变,此步骤会直接使用缓存,无需重新安装依赖,提升构建速度
COPY requirements.txt .
# 4. 安装Python项目依赖
# --no-cache-dir:不缓存pip安装包,减小镜像体积
# -i 可选:指定国内PyPI源(如清华源),解决依赖安装慢/失败问题
RUN pip install --no-cache-dir -r requirements.txt \
-i https://pypi.tuna.tsinghua.edu.cn/simple
# 5. 复制本地所有项目代码到容器工作目录
# 注意:避免复制无用文件(如__pycache__、.env、日志文件),后续用.dockerignore排除
COPY . .
# 将依赖目录添加到Python环境变量(让Python能找到依赖)
ENV LD_LIBRARY_PATH=/usr/local/lib/python3.8/site-packages/dmpython.libs:/usr/local/lib/python3.8/site-packages/dmssl/
# 6. 设置容器启动命令(镜像运行为容器时,默认执行的命令)
# 格式:CMD ["python", "脚本名.py"],数组形式为Docker推荐写法(避免shell解析问题)
CMD ["python", "app.py"]
# cat requirements.txt
dmPython==2.5.30
# cat .
./ ../ .dockerignore
# cat .
./ ../ .dockerignore
# cat .dockerignore
# 排除Python编译缓存
__pycache__/
*.pyc
*.pyo
*.pyd
# 排除虚拟环境
venv/
env/
.venv/
# 排除日志、临时文件
*.log
tmp/
temp/
# 排除Docker相关文件
.dockerignore
Dockerfile~
# 排除敏感配置文件
.env
config.ini.local
# 排除操作系统文件
.DS_Store
Thumbs.db
# pwd
/root/dmpython_test
# ls
app.py Dockerfile requirements.txt
#
报错处理
1. Error: [CODE:-70089]Encryption module failed to load
【原因分析】读取动态依赖库失败
【解决办法】配置环境变量LD_LIBRARY_PATH 并检查依赖库是否齐全
ENV LD_LIBRARY_PATH=/usr/local/lib/python3.8/site-packages/dmpython.libs:/usr/local/lib/python3.8/site-packages/dmssl/
Error: [CODE:-70089]Encryption module failed to load
Traceback (most recent call last):
File "app.py", line 35, in <module>
test_dmsession()
File "app.py", line 28, in test_dmsession
cursor.close()
UnboundLocalError: local variable 'cursor' referenced before assignment