使用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") 
<cx_Oracle.Cursor on <cx_Oracle.Connection to c##[email protected]: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##[email protected]:1521/ORCL>>
>>> cursor.fetchall()
[(1, '張三'), (2, '李四'), (3, '王五')]

可以鏈接,制作結束

以上就是使用Docker制作Python環境連接Oracle鏡像的詳細內容,更多關於docker鏡像Python環境連接Oracle的資料請關註WalkonNet其它相關文章!

推薦閱讀: