1. 首页
  2. 技术知识

docker镜像alpine中安装oracle客户端

目录

    1.背景2.下载instant_client程序包3.Dockerfile

      说明:

    4.遇到的问题

      4.1.找不到libclntsh.so动态连接库4.2.找不到libaio.so.1动态连接库4.3.找不到libnsl.so.1动态连接库

1.背景

有项目需使用python连接oracle数据库,然后查询一些数据进行分析。在安装oracle客户端驱动过程中遇到了一些问题,在此记录下来分享读者。

一点限制:

    oracle数据库与本应用程序不在同一台机器上,数据连接为远程访问方式,针对同一台机器的应用访问,网上有很多;本应用支行在docker容器中,镜像基于alpine:3.7版本编译,针对在ubuntu中的安装网上有很多;基础信息:alping3.7 + python3.6.5 + cx_Oracle7.0.0 + instantclient-basic-linux.x64-11.2.0.4.0

搭建目标:

使用python -c “import cx_Oracle as ora; ora.connect(‘xxx’)”可正常连接oracle数据库


2.下载instant_client程序包

可直接从oracle 官网下载,本应用下载了【instantclient-basic-linux.x64-11.2.0.4.0.zip】包。原因为最好与oracle数据库版本对应。

该压缩包中有instanclient_11_2目录,其目录结构:

  1. ./instantclient_11_2:
  2. |—BASIC_README
  3. |—adrci
  4. |—genezi
  5. |—libclntsh.so.11.1
  6. |—libnnz11.so
  7. |—libocci.so.11.1
  8. |—libociei.so
  9. |—libocijdbc11.so
  10. |—ojdbc5.jar
  11. |—ojdbc6.jar
  12. |—uidrvci
  13. |—xstreeams.jar

复制代码 即包中为oracle客户端连接数据库的所需的库。

3.Dockerfile

直接先上代码然后再说明其中的关键点:

  1. FROM alpine:3.7
  2. ENV ALPINE_VERSION=3.7
  3. #### packages from https://pkgs.alpinelinux.org/packages
  4. # These are always installed. Notes:
  5. #   * dumb-init: a proper init system for containers, to reap zombie children
  6. #   * bash: For entrypoint, and debugging
  7. #   * ca-certificates: for SSL verification during Pip and easy_install
  8. #   * python: the binaries themselves
  9. #   * openblas: required for numpy.
  10. #   * libaio libnsl: for cx_Oracle
  11. ENV PACKAGES=”\
  12.   dumb-init \
  13.   bash vim tini \
  14.   ca-certificates \
  15.   python3==3.6.5-r0 \
  16.   openblas \
  17.   libaio libnsl \
  18. # These packages are not installed immediately, but are added at runtime or ONBUILD to shrink the image as much as possible. Notes:
  19. #   * build-base: used so we include the basic development packages (gcc)
  20. #   * linux-headers: commonly needed, and an unusual package name from Alpine.
  21. ENV BUILD_PACKAGES=”\
  22.   build-base \
  23.   linux-headers \
  24. ## for install oracle instant client
  25. ## from https://oracle.github.io/odpi/doc/installation.html#linux
  26. ENV TNS_ADMIN=/oracle_client/instantclient_11_2
  27. ENV NLS_LANG=SIMPLIFTED_CHINESE_CHINA_ZHS16GBK
  28. ENV LD_LIBRARY_PATH=/oracle_client/instantclient_11_2
  29. RUN echo \
  30.   # 1.install oracle client and create soft link
  31.   && mkdir /oracle_client && cd /oracle_client \
  32.   && wget -O client.zip “https://raw.githubusercontent.com/tianxiawuzhe/alpine37-py365-django21-ai/master/instantclient-basic-linux.x64-11.2.0.4.0.zip” \
  33.   && unzip client.zip && rm client.zip \
  34.   && cd /oracle_client/instantclient_11_2 \
  35.   && ln -s libclntsh.so.11.1  libclntsh.so \
  36.   && ln -s /usr/lib/libnsl.so.2.0.0  /usr/lib/libnsl.so.1 \
  37.   # 2.replacing default repositories with edge ones
  38.   && echo “http://dl-cdn.alpinelinux.org/alpine/edge/testing” >> /etc/apk/repositories \
  39.   && echo “http://dl-cdn.alpinelinux.org/alpine/edge/community” >> /etc/apk/repositories \
  40.   && echo “http://dl-cdn.alpinelinux.org/alpine/edge/main” >> /etc/apk/repositories \
  41. # 3.Add the build packages, and then will be deleted
  42.   && apk add –no-cache –virtual=.build-deps $BUILD_PACKAGES \
  43. # 4.Add the packages, with a CDN-breakage fallback if needed
  44.   && apk add –no-cache $PACKAGES || \
  45.     (sed -i -e ‘s/dl-cdn/dl-4/g’ /etc/apk/repositories && apk add –no-cache $PACKAGES) \
  46.   # 5.make some useful symlinks that are expected to exist
  47.   && cd /usr/bin \
  48.   && { [[ -e idle ]] || ln -s idle3 idle; } \
  49.   && { [[ -e pydoc ]] || ln -s pydoc3 pydoc; } \
  50.   && { [[ -e python ]] || ln -sf python3.6 python; } \
  51.   && { [[ -e python-config ]] || ln -sf python3-config python-config; } \
  52.   && { [[ -e pip ]] || ln -sf pip3 pip; } \
  53.   && ls -l idle pydoc python* pip* \
  54.   && python -m pip install –upgrade –no-cache-dir pip \
  55.   && ls -l idle pydoc python* pip* \
  56.   # 6.install my APP software
  57.   && pip install –no-cache-dir cx_Oracle \
  58.   # 7.End
  59.   && apk del .build-deps \
  60.   && ls -l idle pydoc python* pip* \
  61.   && echo
  62. EXPOSE 8080
  63. ENTRYPOINT tail -f /dev/null
  64. CMD [“/bin/bash”]

复制代码
说明:

01)使用环境变量PACKAGES和BUILD_PACKAGES来区分应用运行时和编译安装时所需要的库,在RUN命令最后删除了BUILD_PACKAGES的依赖库,从而保证镜像尽可能的小;

02)PACKAGES中含有libaio和libnsl两个依赖库,此2个依赖库为cx_Oracle程序包所需的库(更具体的是instant_client驱动包中libclntsh.so.11.1库文件依赖这两个包);

03)LD_LIBRARY_PATH环境变量保证在python程序运行时能够找到instant_client的驱动包位置,本文中是将驱动包解压至容器的/oracle_client/目录下(解压过程见后面的RUN里的unzip);

04)RUN中第1步,是从github上下载上面的驱动包。原因:本应用编译时是使用公共云平台进行编译,在编译过程中绑定了github,从github上读取Dockerfile和*.zip文件进行编译。若是在本地编译镜像,可以使用COPY或ADD来添加文件,但这样可能会让镜像的大小增加,理由是COPY和ADD不能自动解压缩zip,即使在RUN的后面写上rm *.zip,也无法降低镜像的大小,更多信息请查询镜像层的原理。

05)RUN第1步,【ln -s libclntsh.so.11.1 libclntsh.so】是建立驱动包中软连接,这样cx_Oracle在寻找动态连接库时就能找到了,如果不软连接或重命名,那么cx_Oracle将会找不到该库;【ln -s /usr/lib/libnsl.so.2.0.0 /usr/lib/libnsl.so.1】建立libnsl库的软连接,当libclntsh库在运行时会查找此库;

06)RUN第2步,向/etc/apk/repositories文件中添加一些alpine特有的库地址,后面在安装时就能自动下载并安装了;

07)RUN第3步,安装编译过程中所需的依赖包;

08)RUN第4步,安装运行过程中所需的依赖包,此2步可调换顺序,但个人觉得应该这个顺序,因为如果编译的依赖包覆盖了运行所需的包,那么在删除BUILD_PACKAGES后,程序运行时就会出现问题;

09)RUN第5步,建立一些python中简单的软连接,方便后面使用;同时更新了pip的版本;

10)RUN第6步,使用pip安装cx_Oracle程序包;

11)RUN第7步,清理BUILD_PACKAGES这些依赖包;

12)最后就是暴露的端口和启动脚本了;

4.遇到的问题

以下遇到的问题,均是通过执行【python -c “import cx_Oracle as o; o.connect(‘xxx’)”】来验证是否客户端安装成功。

4.1.找不到libclntsh.so动态连接库

详细信息:

cx_Oracle.DatabaseError:

DPI-1047: 64-bit Oracle Client library cannot be loaded:

“Error loading shared library libclntsh.so: No such file or directory”.

See https://oracle.github.io/odpi/doc/installation.html#linux for help

原因有2:

    未正确设置LD_LIBRARY_PATH环境变量,导致python在加载应用时,操作系统未设置正确的库路径;未设置软连接libclntsh.so指向libclntsh.so.11.1,也会导致无法加载;

诊断方法:

在python命令行里,手工【from ctypes import find_library as f, CDLL】,尝试使用find_library来查找动态连接库,如果能用此命令找到,比如f(‘libclntsh.so.11.1’)可以找到,但f(‘libclntsh.so’)找不到,说明LD_LIBRARY_PATH设置正确了,只是没有软连接。然后再用CDLL尝试加载动态连接库,看中间加载是否会出问题。

4.2.找不到libaio.so.1动态连接库

请确认libaio是否安装成功,安装成功后,应该在/usr/lib/libaio.so.1.0.1文件,同时会存在libaio.so.1的软连接。

4.3.找不到libnsl.so.1动态连接库

由于当前libnsl的版本已经是libns.so.2.0.0,因此在安装libnsl后会自动存在libnsl.so.2的软连接,而本应用中oracle的驱动版本是较老的,因此直接手工建立了软连接【ln -s /usr/lib/libnsl.so.2.0.0 /usr/lib/libnsl.so.1】,试验成功!

以上就是docker镜像alpine中安装oracle客户端的详细内容,更多关于docker镜像alpine安装oracle的资料请关注软件技术网其它相关文章!

原创文章,作者:starterknow,如若转载,请注明出处:https://www.starterknow.com/109176.html

联系我们