docker镜像Python环境连接Oracle

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

docker镜像Python环境连接Oracle

xiaogp   2022-09-08 我要评论

Python连接Oracle本地测试

依赖安装准备

Python、链接Oracle需要Python依赖和本地Oracle客户端,测试环境Oracle版本12.1.0.2.0,开发和测试环境为linux,先安装linux客户端,选择zip解压免安装版本

Oracle linux客户端

解压到某个目录

unzip instantclient-basic-linux.x64-12.1.0.2.0.zip

解压后新建/network/admin文件夹

cd /opt/instantclient_12_1/
mkdir -p /network/admin

修改root用户的环境变量

vim /etc/profile
export ORACLE_HOME=/opt/instantclient_12_1
export TNS_ADMIN=$ORACLE_HOME/network/admin
export NLS_LANG="SIMPLIFIED CHINESE_CHINA".ZHS16GBK
export NLS_DATE_FORMAT="yyyy-mm-dd hh24:mi:ss"
export LD_LIBRARY_PATH=$ORACLE_HOME:$LD_LIBRARY_PATH
export PATH=$ORACLE_HOME:$PATH
source /etc/profile

下一步安装Python依赖

pip install cx_Oracle

Python脚本测试

root@ubuntu:~# python
Python 3.7.6 (default, Jan  8 2020, 19:59:22) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle as cx
>>> con = cx.connect('username', 'password', 'xxx.xxx.xxx.xxx:1521/ORCL')
>>> cursor = con.cursor() 
>>> cursor.execute("select * from emp") 
<cx_Oracle.Cursor on <cx_Oracle.Connection to c##als770ud1@192.168.61.79:1521/ORCL>>
>>> cursor.fetchall()
[(1, '张三'), (2, '李四'), (3, '王五')]
>>> 

制作Docker镜像

创建Dockerfile

touch Dockerfile
# 将oracle本地客户端文件夹移动到同一级目录
cp -r /opt/instantclient_12_1/ ./

Dockerfile

FROM python:3.7
ENV  PIPURL "https://mirrors.aliyun.com/pypi/simple/"
RUN pip install cx_Oracle --default-timeout=1000
COPY instantclient_12_1 /opt/instantclient_12_1
ENV ORACLE_HOME=/opt/instantclient_12_1
ENV TNS_ADMIN=$ORACLE_HOME/network/admin
ENV NLS_LANG="SIMPLIFIED CHINESE_CHINA".ZHS16GBK
ENV NLS_DATE_FORMAT="yyyy-mm-dd hh24:mi:ss"
ENV LD_LIBRARY_PATH=$ORACLE_HOME:$LD_LIBRARY_PATH
ENV PATH=$ORACLE_HOME:$PATH
RUN apt-get update
RUN apt-get install -y libaio1

镜像构建

docker build -t xiaogp/python_oraqcle:v3 .

构建完成

root@ubuntu:~/docker/PYTHON_ORACLE# docker images
REPOSITORY                              TAG                            IMAGE ID            CREATED             SIZE
xiaogp/python_oraqcle                    v3                             bb0100d9c3f5        39 seconds ago      1.1GB

启动镜像测试一下

root@ubuntu:~/docker/PYTHON_ORACLE# docker run -it bb0100d9c3f5 /bin/bash
root@fbff875ba4d5:/# python
Python 3.7.9 (default, Jan 12 2021, 17:26:22) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle as cx
>>> con = cx.connect('username', 'password', 'xxx.xxx.xxx.xxx:1521/ORCL')
>>> cursor = con.cursor()
>>> cursor.execute("select * from emp")
<cx_Oracle.Cursor on <cx_Oracle.Connection to c##als770ud1@192.168.61.79:1521/ORCL>>
>>> cursor.fetchall()
[(1, '张三'), (2, '李四'), (3, '王五')]

可以链接,制作结束

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们