Search code examples
c++oracledockeralpine-linuxinstantclient

How to compile and run an Oracle OCI/OCCI program with C++ in Alpine


I have some problems trying to compile (more precisely, link) an OCCI program with C++ in an Alpine docker. I already have installed Instant Client 21.14 following some of the steps in this question, with the symbolic links already created and the environment variables set. My problem comes when I try to link a simple C++ program like the following:

g++ -o oracleclient OracleClient2.o -L"$ORACLE_HOME" -locci -lclntsh
/// Or
g++ -o oracleclient OracleClient2.o -L"$ORACLE_HOME" -locci_gcc53 -lclntsh

This compilations steps works in Ubuntu, but in Alpine I get a lot of linking errors, some of them are:

ld: warning: libdl.so.2, needed by /usr/lib/instantclient/libocci_gcc53.so, not found (try using -rpath or -rpath-link)
ld: /usr/lib/instantclient/libclntsh.so: undefined reference to `sigfillset@GLIBC_2.2.5'
ld: /usr/lib/instantclient/libocci_gcc53.so: undefined reference to `operator new(unsigned long)@GLIBCXX_3.4'
ld: /usr/lib/instantclient/libclntsh.so: undefined reference to `sigprocmask@GLIBC_2.2.5'
ld: /usr/lib/instantclient/libclntsh.so: undefined reference to `pthread_cond_timedwait@GLIBC_2.3.2'
ld: /usr/lib/instantclient/libclntshcore.so.21.1: undefined reference to `pthread_attr_setschedpolicy@GLIBC_2.2.5'

First, I don't know how to install libdl.so.2 in Alpine, and the second issue is that to my understanding, Alpine uses musl-libc and the Oracle shared libraries uses glibc, so they are incompatible. Already tried to install gcompat but it didn't solve my problem.

Any help is appreciated.

I tried to link and execute an OCCI C++ app in an Alpine docker but apparently the shared libraries of Instant Client are incompatible with Alpine.


Solution

  • One of the things that makes Alpine Linux setups smaller is use of an alternate system C library (musl libc rather than GNU libc). Some of the libraries you're seeing like libdl.so.2 are part of that core C library. That's very hard to replace.

    If you're seeing link errors referring to some of these system libraries, or explicit references to symbolname@GLIBC_... version markers, then your application probably can't run on Alpine at all. Almost every other Linux distribution (including Debian, Ubuntu, and RHEL) is based on GNU libc, and you won't have these link errors on that other distribution.

    There's some Alpine documentation on Running glibc programs but they largely boil down to complex emulation layers. That page eventually starts getting down to using chroot environments and containers as good ways to run programs that require GNU libc in an otherwise Alpine-based environment, at which point you're not really using your base Alpine container any more.

    So, I'd change your image to be based on another Linux distribution, for example

    FROM debian:bookworm AS build
    RUN apt-get update \
     && DEBIAN_FRONTEND=noninteractive \
        apt-get install --assume-yes --no-install-recommends \
          build-essential
    ...
    RUN g++ -o oracleclient OracleClient2.o -L/opt/oracle/lib -locci -lclntsh
    
    FROM debian:bookworm
    ...
    COPY --from=build /app/oracleclient /usr/local/lib
    CMD ["oracleclient"]