Docker build cmake multiple containers with same custom library

  c#, cmake, docker, docker-compose, shared-libraries

I’ll start off by saying that I’m new to Docker and a novice with cmake so forgive any ignorance. I’m working on porting a series of applications and a single shared library to a docker environment. From my understanding Docker does not play well with shared libraries so I’d need to static link. My goal is to use docker-compose to build multiple different application containers but each of these containers using the same custom library (that was setup as a .so in previous environment). I’m currently setup to do cross-compile w/ Docker and can build and run each app w/o the library. Now I need to add in the library.

Current folder/file setup is as follows:

app1:
  inc:
    app1.h <--includes mysharedlib.h
  src:
    app1.cpp
  CMakeLists.txt
  custom-toolchain.cmake
  Dockerfile

app2:
  inc:
    app2.h <--includes mysharedlib.h
  src:
    app2.cpp
  CMakeLists.txt
  custom-toolchain.cmake
  Dockerfile

MySharedLib:
  inc:
    mysharedlib.h
  src:
    mysharedlib.cpp
  custom-toolchain.cmake
  CMakeLists.txt

Can someone please help explain how I go about including the library into each container and how to include it correctly with CMake?

current working app implementation is as follows:
dockerfile for app1:

ARG IMAGE_TAG=2-bullseye
ARG TOOLCHAIN_ARCH=aarch64
ARG PKG_ARCH=arm64
ARG IMAGE_ARCH=linux/arm64

FROM torizon/debian-cross-toolchain-arm64:2-bullseye AS build

ARG PKG_ARCH
ARG TOOLCHAIN_ARCH

RUN apt-get -y update && apt-get install -y 
    cmake 
    python-dev 
    gcc 
    make 
    gcc 
    binutils 
    build-essential 
    wget
    libasound2-dev:arm64 
    && apt-get clean && apt-get autoremove && rm -rf /var/lib/apt/lists/*

WORKDIR /mypjsip
RUN mkdir out_libs

RUN wget https://github.com/pjsip/pjproject/archive/2.10.tar.gz && tar -xvf 2.10.tar.gz
RUN cd pjproject-2.10/ && ./configure --host=aarch64-linux-gnu --disable-libwebrtc  && make dep && make && make DESTDIR=/mypjsip/out_libs install

COPY . app1_src
COPY $TOOLCHAIN_ARCH-toolchain.cmake app1_src/$TOOLCHAIN_ARCH-toolchain.cmake
RUN mkdir app1_src/build
RUN cd app1_src/build && cmake .. -DCMAKE_TOOLCHAIN_FILE=./$TOOLCHAIN_ARCH-toolchain.cmake
RUN cd app1_src/build && make install

FROM --platform=linux/arm64 torizon/debian:$IMAGE_TAG
RUN apt-get -y update && apt-get install -y 
    libasound2 
    alsa-utils 
    && apt-get clean && apt-get autoremove && rm -rf /var/lib/apt/lists/*

COPY --from=build /mypjsip/pjproject-2.10/pjsip-apps/bin/pjsua* /usr/local/bin/pjsua
COPY --from=build /mypjsip/out_libs/* /
COPY --from=build /usr/bin/app1 /usr/bin/

ENTRYPOINT [ "/usr/bin/app1" ]

cmake for app1:

cmake_minimum_required(VERSION 3.13)

project(app1)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)

set(INC_DIR ./inc)
include_directories(${INC_DIR})

AUX_SOURCE_DIRECTORY(src SOURCE_FILES)

add_executable(app1 ${SOURCE_FILES})

set(WARNINGS "
-pedantic 
-pedantic-errors 
-Wextra 
-Wcast-align 
-Wcast-qual 
-Wdisabled-optimization 
-Werror 
-Wfloat-equal 
-Wformat=2 
-Wformat-nonliteral 
-Wformat-security  
-Wformat-y2k 
-Wimport 
-Winit-self 
-Winline 
-Winvalid-pch 
-Wmissing-field-initializers 
-Wmissing-format-attribute 
-Wmissing-include-dirs 
-Wmissing-noreturn 
-Wpacked 
-Wpointer-arith 
-Wredundant-decls 
-Wshadow 
-Wstack-protector 
-Wstrict-aliasing=2 
-Wswitch-default 
-Wswitch-enum 
-Wunreachable-code 
-Wvariadic-macros 
-Wwrite-strings")


if ( CMAKE_COMPILER_IS_GNUCXX )
    set(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} ${WARNINGS} ")
endif(CMAKE_COMPILER_IS_GNUCXX)

set(CMAKE_INSTALL_PREFIX /usr)
install(TARGETS app1 DESTINATION bin)

Source: Docker Questions

LEAVE A COMMENT