Docker镜像alpine中安装oracle客户端


使用Go编写的程序需要连接Oralce数据,通过Docker编译成镜像。以Alpine作为基础镜像。记录下遇到的问题备查

环境信息

go version go1.19 darwin/amd64
oracle 21.8.0.0.0
alpine 3.16

Dockerfile

FROM ponycool/alpine-3.16:latest

ENV ORACLE_PATH=/opt/oracle
ENV ORACLE_VERSION=21.8.0.0.0
ENV LD_LIBRARY_PATH=$ORACLE_PATH

ARG APP_PATH=/opt/perf

RUN apk update && mkdir -p ${APP_PATH} \
    && apk add --no-cache wget libaio libnsl libc6-compat libgcc libstdc++ \
    && cd /opt \
    && wget -O instantclient.zip https://download.oracle.com/otn_software/linux/instantclient/218000/instantclient-basic-linux.x64-${ORACLE_VERSION}dbru.zip \
    && unzip instantclient.zip && mv instantclient_21_8 oracle \
    && ln -s /lib/libc.so.6 /usr/lib/libresolv.so.2 \
    && ln -s /lib/libc.musl-x86_64.so.1 /lib/ld-linux-x86-64.so.2 \
    && echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH" >> /etc/profile.d/oracle_client.sh \
    && echo "export PATH=$LD_LIBRARY_PATH:$PATH" >> /etc/profile.d/oracle_client.sh \
    && rm -rf /opt/instantclient.zip \
    && rm -rf /var/cache/apk/*  \
    && apk del wget

ADD task ${APP_PATH}/task

WORKDIR /opt/perf

ENTRYPOINT ["/opt/perf/task"]

1. 报错:"Error loading shared library libresolv.so.2: No such file or directory"

2022-11-21 16:00:28    ERROR    runtime/panic.go:884    ORA-00000: DPI-1047: Cannot locate a 64-bit Oracle Client library: "Error loading shared library libresolv.so.2: No such file or directory (needed by /opt/oracle/instantclient/libclntsh.so)". See https://oracle.github.io/odpi/doc/installation.html#linux for help
runtime.gopanic
    /usr/local/go/src/runtime/panic.go:884
github.com/ponycool/nebula-lib/db.OracleInit
    /root/go/pkg/mod/github.com/ponycool/nebula-lib@v0.0.0-20221115072311-70abd77047ee/db/oracle.go:48
main.dbInit
    /builds/c2c/perf/task/main.go:125
main.main
    /builds/c2c/perf/task/main.go:25
runtime.main
    /usr/local/go/src/runtime/proc.go:250
panic: ORA-00000: DPI-1047: Cannot locate a 64-bit Oracle Client library: "Error loading shared library libresolv.so.2: No such file or directory (needed by /opt/oracle/instantclient/libclntsh.so)". See https://oracle.github.io/odpi/doc/installation.html#linux for help

goroutine 1 [running]:
github.com/ponycool/nebula-lib/db.OracleInit({0x3, {0xc0003ba040, 0x38}, 0x0, 0x0, 0x0, 0x0}, 0xc00022d180)
    /root/go/pkg/mod/github.com/ponycool/nebula-lib@v0.0.0-20221115072311-70abd77047ee/db/oracle.go:48 +0x20b
main.dbInit()
    /builds/c2c/perf/task/main.go:125 +0x5d0
main.main()
    /builds/c2c/perf/task/main.go:25 +0x2a

解决
创建libresolv.so.2软连接

ln -s /lib/libc.so.6  /usr/lib/libresolv.so.2

2. 报错:"Error loading shared library ld-linux-x86-64.so.2: No such file or directory"

2022-11-22 09:48:59    ERROR    runtime/panic.go:884    ORA-00000: DPI-1047: Cannot locate a 64-bit Oracle Client library: "Error loading shared library ld-linux-x86-64.so.2: No such file or directory (needed by /opt/oracle/libclntsh.so)". See https://oracle.github.io/odpi/doc/installation.html#linux for help
runtime.gopanic
    /usr/local/go/src/runtime/panic.go:884
github.com/ponycool/nebula-lib/db.OracleInit
    /root/go/pkg/mod/github.com/ponycool/nebula-lib@v0.0.0-20221115072311-70abd77047ee/db/oracle.go:48
main.dbInit
    /builds/c2c/perf/task/main.go:125
main.main
    /builds/c2c/perf/task/main.go:25
runtime.main
    /usr/local/go/src/runtime/proc.go:250
panic: ORA-00000: DPI-1047: Cannot locate a 64-bit Oracle Client library: "Error loading shared library ld-linux-x86-64.so.2: No such file or directory (needed by /opt/oracle/libclntsh.so)". See https://oracle.github.io/odpi/doc/installation.html#linux for help

goroutine 1 [running]:
github.com/ponycool/nebula-lib/db.OracleInit({0x3, {0xc0003be000, 0x38}, 0x0, 0x0, 0x0, 0x0}, 0xc00022d180)
    /root/go/pkg/mod/github.com/ponycool/nebula-lib@v0.0.0-20221115072311-70abd77047ee/db/oracle.go:48 +0x20b
main.dbInit()
    /builds/c2c/perf/task/main.go:125 +0x5d0
main.main()
    /builds/c2c/perf/task/main.go:25 +0x2a

解决
安装libgcc、libstdc++并创建软连接

ln -s /lib/libc.musl-x86_64.so.1 /lib/ld-linux-x86-64.so.2

3. 报错:"syscall, locked to thread"

goroutine 1 [syscall, locked to thread]:
runtime.cgocall(0x9fd620, 0xc000122530)
    /usr/local/go/src/runtime/cgocall.go:158 +0x5c fp=0xc000122508 sp=0xc0001224d0 pc=0x41143c
github.com/godror/godror._Cfunc_dpiPool_create(0x7fed1fd83e30, 0x7fed5660ae90, 0x3, 0x7fed5660acd0, 0x6, 0x7fed1fd83e70, 0x2d, 0xc0003280f0, 0xc0003022a0, 0xc000306080)
    _cgo_gotypes.go:4469 +0x4c fp=0xc000122530 sp=0xc000122508 pc=0x9248cc
github.com/godror/godror.(*drv).createPool.func5.1(0x7000000000?, 0x7fed2ef51190?, 0x10?, 0x7fed56533f18?, 0x10?, 0x7fed2ef51328?, 0x8?, 0x7fed56533f18?)
    /root/go/pkg/mod/github.com/godror/godror@v0.34.0/drv.go:774 +0x165 fp=0xc0001225e8 sp=0xc000122530 pc=0x93a005
github.com/godror/godror.(*drv).createPool.func5()
    /root/go/pkg/mod/github.com/godror/godror@v0.34.0/drv.go:774 +0x39 fp=0xc000122638 sp=0xc0001225e8 pc=0x939e79
github.com/godror/godror.(*drv).checkExecNoLOT(0x41b067?, 0x8?)
    /root/go/pkg/mod/github.com/godror/godror@v0.34.0/drv.go:254 +0x22 fp=0xc000122658 sp=0xc000122638 pc=0x935442
github.com/godror/godror.(*drv).checkExec(0x7fed1fd83e70?, 0x2d?)
    /root/go/pkg/mod/github.com/godror/godror@v0.34.0/drv.go:248 +0x34 fp=0xc000122688 sp=0xc000122658 pc=0x935394
github.com/godror/godror.(*drv).createPool(_, {{{0xc0003b2000, 0x3}, {0xc0003b200b, 0x2d}, {{0xc0003b2004, 0x6}}, {0x0, 0x0}, {0x0, ...}, ...}, ...})
    /root/go/pkg/mod/github.com/godror/godror@v0.34.0/drv.go:765 +0x796 fp=0xc0001228b0 sp=0xc000122688 pc=0x939a76
github.com/godror/godror.(*drv).getPool(_, {{{0xc0003b2000, 0x3}, {0xc0003b200b, 0x2d}, {{0xc0003b2004, 0x6}}, {0x0, 0x0}, {0x0, ...}, ...}, ...})
    /root/go/pkg/mod/github.com/godror/godror@v0.34.0/drv.go:657 +0x57e fp=0xc000122b60 sp=0xc0001228b0 pc=0x938f9e
github.com/godror/godror.(*drv).createConnFromParams(_, {{{0xc0003b2000, 0x3}, {0xc0003b200b, 0x2d}, {{0xc0003b2004, 0x6}}, {0x0, 0x0}, {0x0, ...}, ...}, ...})
    /root/go/pkg/mod/github.com/godror/godror@v0.34.0/drv.go:592 +0x198 fp=0xc000123010 sp=0xc000122b60 pc=0x9387d8
github.com/godror/godror.connector.Connect({0xfe1800, {{{0xc0003b2000, 0x3}, {0xc0003b200b, 0x2d}, {{...}}, {0x0, 0x0}, {0x0, 0x0}, ...}, ...}}, ...)
    /root/go/pkg/mod/github.com/godror/godror@v0.34.0/drv.go:1113 +0x7f8 fp=0xc0001237c8 sp=0xc000123010 pc=0x93c938
github.com/godror/godror.(*connector).Connect(0xc0002fa6c0?, {0xc17e70?, 0xc00002a048?})
    <autogenerated>:1 +0x9e fp=0xc000123aa8 sp=0xc0001237c8 pc=0x986b9e
database/sql.(*DB).conn(0xc00039c820, {0xc17e70, 0xc00002a048}, 0x1)
    /usr/local/go/src/database/sql/sql.go:1393 +0x763 fp=0xc000123c20 sp=0xc000123aa8 pc=0x797f23
database/sql.(*DB).PingContext(0xb1c21b?, {0xc17e70, 0xc00002a048})
    /usr/local/go/src/database/sql/sql.go:851 +0x78 fp=0xc000123c90 sp=0xc000123c20 pc=0x795ab8
database/sql.(*DB).Ping(...)
    /usr/local/go/src/database/sql/sql.go:873
github.com/ponycool/nebula-lib/db.OracleInit({0x3, {0xc0003b2000, 0x38}, 0x0, 0x0, 0x0, 0x0}, 0xc00022f180)
    /root/go/pkg/mod/github.com/ponycool/nebula-lib@v0.0.0-20221115072311-70abd77047ee/db/oracle.go:44 +0x145 fp=0xc000123d28 sp=0xc000123c90 pc=0x9bb405
main.dbInit()
    /builds/c2c/perf/task/main.go:125 +0x5d0 fp=0xc000123f18 sp=0xc000123d28 pc=0x9d16f0
main.main()
    /builds/c2c/perf/task/main.go:25 +0x2a fp=0xc000123f80 sp=0xc000123f18 pc=0x9d078a
runtime.main()
    /usr/local/go/src/runtime/proc.go:250 +0x212 fp=0xc000123fe0 sp=0xc000123f80 pc=0x445df2
runtime.goexit()
    /usr/local/go/src/runtime/asm_amd64.s:1594 +0x1 fp=0xc000123fe8 sp=0xc000123fe0 pc=0x4741c1

此问题的具体表现为,执行编写的程序时,偶尔可以正常运行。通过错误判断好像是进程被锁,暂时没有找到解决的方法。如果有解决方法欢迎在评论留言。

Oracle的限制比较多尽量使用官方提供的镜像。
Oracle 推荐Docker镜像

参考

Oracle客户端安装文档

声明:初心|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - Docker镜像alpine中安装oracle客户端


愿你勿忘初心,并从一而终