教你如何使用Docker制作Python环境连接Oracle镜像

本文详细介绍了如何在Docker环境下配置Python连接Oracle数据库的步骤,包括安装Oracle本地客户端、设置环境变量、安装Python cx_Oracle依赖以及构建Docker镜像。通过这些步骤,可以在Docker容器中成功运行Python脚本并连接到Oracle数据库进行数据操作。
摘要由CSDN通过智能技术生成
这篇文章主要为大家介绍了使用Docker制作Python环境连接Oracle镜像示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

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") 
>
>>> 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")
>
>>> cursor.fetchall()
[(1, '张三'), (2, '李四'), (3, '王五')]

可以链接,制作结束

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值