使用Docker制作Python环境连接Oracle镜像

Tricia ·
更新时间:2024-11-14
· 1685 次阅读

目录

Python连接Oracle本地测试

依赖安装准备

制作Docker镜像

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, '王五')]

可以链接,制作结束

以上就是使用Docker制作Python环境连接Oracle镜像的详细内容,更多关于docker镜像Python环境连接Oracle的资料请关注软件开发网其它相关文章!



python环境 Oracle Docker Python

需要 登录 后方可回复, 如果你还没有账号请 注册新账号