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

作者:xiaogp 时间:2021-02-21 23:42:47 

Python连接Oracle本地测试

依赖安装准备

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

Oracle linux客户端

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

解压到某个目录

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

可以链接,制作结束

来源:https://www.jianshu.com/p/432801a6e73f

标签:docker,镜像,Python,连接,Oracle
0
投稿

猜你喜欢

  • 基于JS实现html中placeholder属性提示文字效果示例

    2023-09-07 22:50:58
  • Python实现递归遍历文件夹并删除文件

    2021-09-06 09:51:17
  • Django 返回json数据的实现示例

    2021-03-06 21:33:17
  • Google logo “我的中国”谷歌国际少年绘画大赛小学1-3年级

    2008-12-19 12:26:00
  • Django实现发送邮件功能

    2021-05-13 10:25:44
  • Python requests接口测试实现代码

    2023-09-10 18:09:21
  • PyCharm安装库numpy失败问题的详细解决方法

    2023-05-18 22:45:16
  • python包导入的两种方式

    2022-11-25 22:49:30
  • 电子商务搜索LIST页面用户体验设计

    2010-08-03 12:57:00
  • MATLAB中print函数使用示例详解

    2023-11-18 04:18:04
  • python scrapy重复执行实现代码详解

    2023-01-15 17:27:37
  • Python 实现Serial 与STM32J进行串口通讯

    2021-03-05 16:01:33
  • Pycharm配置autopep8实现流程解析

    2021-08-23 05:34:40
  • python3中类的继承以及self和super的区别详解

    2023-02-23 20:31:25
  • jquery常用的表单操作很全很详细

    2011-09-01 19:21:11
  • 在XPath查询中指定轴(转自MSSQL手册)

    2008-09-04 14:23:00
  • 浅谈Python中带_的变量或函数命名

    2023-08-30 15:44:08
  • Matlab中关于argmax、argmin函数的使用解读

    2023-06-27 20:36:53
  • python实操案例练习(七)

    2021-09-23 01:01:03
  • PHP正则表达式替换<pre>标签外的内容

    2023-05-22 10:47:12
  • asp之家 网络编程 m.aspxhome.com